Thursday, April 10, 2014

Help with proper code on printing bitmap pages

Right now, I have some code that takes a snapshot of a grid and put them as bitmaps in a folder. Then I have some code that reads the bitmaps from that folder and prints it out. How do I change the code so that I can transfer the bitmap directly from the first set of code over to the second set of code?


Here is the code that reads the pictures from the folder and then prints them out.



int aa;
string printingname;
List<ImageData> _imageData;

Windows.Graphics.Printing.PrintOrientation orientation;

public PrintingPage()
{
aa = App.numberofgrids;
printingname = App.printingname;

//TimeSpan period = TimeSpan.FromMilliseconds(1000);

this.InitializeComponent();

this._itemsPerPage = 1;

OnLoaded();

OnRegister();

orientation = App.orientation;



InvokePrintButtonClick();
initialize();
SV2_Loaded();

}


public void SV2_Loaded()
{
TimeSpan period = TimeSpan.FromMilliseconds(1000);

Windows.System.Threading.ThreadPoolTimer.CreateTimer(async (source) =>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
page.ChangeView(0, 0, .3f);
});
}
, period);
}




private async void InvokePrintButtonClick()
{
try
{
await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
}

catch
{
return;
}
}

public async void initialize()
{

try
{


int ii = 0;
while (ii < aa)
{


var assetsFolder = ApplicationData.Current.LocalFolder;
StorageFile myImage = await assetsFolder.GetFileAsync(printingname + ii + ".bmp");

if (myImage != null)
{

Windows.Storage.Streams.IRandomAccessStream fileStream = await myImage.OpenAsync(Windows.Storage.FileAccessMode.Read);

Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();

bitmapImage.SetSource(fileStream);

Image displayImage = new Image();

displayImage.Source = bitmapImage;
printingstackpanel.Children.Add(displayImage);
}


ii++;
}
}

catch { return; }
}



public void OnLoaded()
{


StorageFolder testfolder = ApplicationData.Current.LocalFolder;


this._imageData = Enumerable.Range(0, aa).Select(
i => new ImageData()
{
Title = string.Format(""),
ImageUri = string.Format("ms-appdata:///local/" + printingname + "{0}.bmp", i)
}
).ToList();


}
public void OnRegister()
{
// Note, this manager can also invoke the print UI for us.
PrintManager manager = PrintManager.GetForCurrentView();

manager.PrintTaskRequested += OnPrintTaskRequested;

}
void OnPrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// If I need to be asynchronous, I can get a deferral. I don't *need*
// to do this here, I'm just faking it.
var deferral = args.Request.GetDeferral();


PrintTask printTask = args.Request.CreatePrintTask(
"My Print Job",
OnPrintTaskSourceRequestedHandler);

printTask.Options.Orientation = orientation;

printTask.Completed += OnPrintTaskCompleted;

this.AddCustomPrintOption(printTask);

deferral.Complete();
}
void AddCustomPrintOption(PrintTask printTask)
{
/*PrintTaskOptionDetails details = PrintTaskOptionDetails.GetFromPrintTaskOptions(
printTask.Options);

// Clear, string "optionId" needs to be in a constant etc.
PrintCustomItemListOptionDetails formatOptions =
details.CreateItemListOption("optionId", "Images Per Page");

var options = new string[] { "1", "2", "4" };

foreach (var item in options)
{
formatOptions.AddItem(item, item);
}*/

PrintTaskOptionDetails details = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);

PrintCustomItemListOptionDetails pageFormat = details.CreateItemListOption("PageRange", "Page Range");
// Create a new list option
pageFormat.AddItem("PrintAll", "Print all");
//pageFormat.AddItem("PrintSelection", "Print Selection");
//pageFormat.AddItem("PrintRange", "Print Range");

/*details.DisplayedOptions.Add("optionId");

details.OptionChanged += OnItemsPerPageChanged;*/

details.DisplayedOptions.Clear();

details.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
details.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
details.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.ColorMode);

// Add the custom option to the option list
details.DisplayedOptions.Add("PageRange");

// Create new edit option
PrintCustomTextOptionDetails pageRangeEdit = details.CreateTextOption("PageRangeEdit", "Range");

// Register the handler for the option change event
details.OptionChanged += printDetailedOptions_OptionChanged;
}

private bool selectionMode;
private bool pageRangeEditVisible = false;
private int totalPages;
async void printDetailedOptions_OptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
{
if (args.OptionId == null)
return;

string optionId = args.OptionId.ToString();

// Handle change in Page Range Option

if (optionId == "PageRange")
{
IPrintOptionDetails pageRange = sender.Options[optionId];
string pageRangeValue = pageRange.Value.ToString();

selectionMode = false;

switch (pageRangeValue)
{
case "PrintRange":
// Add PageRangeEdit custom option to the option list
sender.DisplayedOptions.Add("PageRangeEdit");
pageRangeEditVisible = true;
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
//ShowContent(null);
});
break;
case "PrintSelection":
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
//ScenarioOutput5 outputContent = (ScenarioOutput5)rootPage.OutputFrame.Content;
//ShowContent(outputContent.SelectedText);
});
//RemovePageRangeEdit(sender);
}
break;
default:
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
//ShowContent(null);
});
//RemovePageRangeEdit(sender);
break;
}

//Refresh();

}
else if (optionId == "PageRangeEdit")
{
IPrintOptionDetails pageRange = sender.Options[optionId];
// Expected range format (p1,p2...)*, (p3-p9)* ...
if (!Regex.IsMatch(pageRange.Value.ToString(), @"^\s*\d+\s*(\-\s*\d+\s*)?(\,\s*\d+\s*(\-\s*\d+\s*)?)*$"))
{
pageRange.ErrorText = "Invalid Page Range (eg: 1-3, 5)";
}
else
{
pageRange.ErrorText = string.Empty;
try
{
//GetPagesInRange(pageRange.Value.ToString());
//Refresh();
}
catch
{
//pageRange.ErrorText = ipex.Message;
}
}
}
}




void OnPrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
{
this._itemsPerPage = 1;
this._pageCount = 0;
this._pageSize = null;
this._imageableRect = null;
this._document = null;
}
async void OnPrintTaskSourceRequestedHandler(PrintTaskSourceRequestedArgs args)
{
var deferral = args.GetDeferral();

await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
this._document = new PrintDocument();

this._document.Paginate += OnPaginate;
this._document.GetPreviewPage += OnGetPreviewPage;
this._document.AddPages += OnAddPages;

// Tell the caller about it.
args.SetSource(this._document.DocumentSource);
}
);
deferral.Complete();
}
void OnAddPages(object sender, AddPagesEventArgs e)
{
// Note: this code does not take notice of any page range specified
// by the user. It should.
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
for (int i = 1; i <= this._pageCount; i++)
{
this._document.AddPage(await this.BuildPageAsync(i));
}
this._document.AddPagesComplete();
}
);
}
void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
// NB: No longer caching these in a dictionary.
UIElement page = await this.BuildPageAsync(e.PageNumber);

this._document.SetPreviewPage(e.PageNumber, page);
}
);
}
async Task<UIElement> BuildPageAsync(int pageNumber)
{
// Account for pages going 1..N rather than 0..N-1
int pageIndex = pageNumber - 1;

// TBD: Unsure about DPI here.
// Changed from a Canvas in the previous code.
Grid parentGrid = new Grid();
parentGrid.Width = this._pageSize.Value.Width;
parentGrid.Height = this._pageSize.Value.Height;

// Make a grid
Grid grid = new Grid();
grid.Margin = new Thickness(
this._imageableRect.Value.Left,
this._imageableRect.Value.Top,
this._pageSize.Value.Width - this._imageableRect.Value.Width - this._imageableRect.Value.Left,
this._pageSize.Value.Height - this._imageableRect.Value.Height - this._imageableRect.Value.Top);

// How many squares?
int squareCount = (int)(Math.Sqrt(this._itemsPerPage));

// Make grid rows and cols
GridLength length = new GridLength(1, GridUnitType.Star);

for (int i = 0; i < squareCount; i++)
{
grid.RowDefinitions.Add(new RowDefinition() { Height = length });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = length });
}

// Make items
int startIndex = this._itemsPerPage * pageIndex;
int endIndex =
Math.Min(startIndex + this._itemsPerPage, this._imageData.Count);

int rowNo = 0;
int colNo = 0;
List<Task> imageLoadTasks = new List<Task>();

for (int itemIndex = startIndex; itemIndex < endIndex; itemIndex++)
{
ImageUserControl control = new ImageUserControl();
Binding binding = new Binding()
{
Path = new PropertyPath("ImageUri")
};
control.SetBinding(ImageUserControl.ImageUriProperty, binding);
control.DataContext = this._imageData[itemIndex];

imageLoadTasks.Add(control.LoadAsync());

Grid.SetRow(control, rowNo);
Grid.SetColumn(control, colNo);

colNo++;

if (colNo >= squareCount)
{
colNo = 0;
rowNo++;
}
grid.Children.Add(control);
}

// Offset it into the printable area
Canvas.SetLeft(grid, this._imageableRect.Value.Left);
Canvas.SetTop(grid, this._imageableRect.Value.Top);

parentGrid.Children.Add(grid);

// Wait for the images to load.
await Task.WhenAll(imageLoadTasks);

return (parentGrid);
}
void OnPaginate(object sender, PaginateEventArgs e)
{
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
this.GetPageSize(e);

int itemCount = this._imageData.Count;
this._pageCount = itemCount / this._itemsPerPage;

if ((itemCount % this._itemsPerPage) > 0)
{
this._pageCount++;
}
this._document.SetPreviewPageCount(this._pageCount,
PreviewPageCountType.Final);
}
);
}
void GetPageSize(PaginateEventArgs e)
{
if (this._pageSize == null)
{
PrintPageDescription description = e.PrintTaskOptions.GetPageDescription(
(uint)e.CurrentPreviewPageNumber);

this._pageSize = description.PageSize;
this._imageableRect = description.ImageableRect;
}
}
int _itemsPerPage;
int _pageCount;
Size? _pageSize;
Rect? _imageableRect;
PrintDocument _document;

private async void button(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
if (sender == printbutton)
{
await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
}

else if (sender == closebutton)
{
CoreWindow.GetForCurrentThread().Close();
}
}

No comments:

Post a Comment