I am trying to save a QR Bar Code as an image and not having much luck. Anyone have a small sample of code to do so? New to WPF and what I have found on the web so far doesn't seem to work for me.
Also if you have any insight into printing that would be great also.
Thanks,
Nick
Hi Nick,
for example if you have one "Save" button then you should implement the following code:
private
{
WriteableBitmap bitmap = new WriteableBitmap((int)BarCode.ActualWidth,
(int)BarCode.ActualHeight);
bitmap.Render(BarCode,
new TranslateTransform
() { X = 0, Y = 0 });
bitmap.Invalidate();
SaveToDisk(bitmap);
}
private void SaveToDisk(WriteableBitmap bitmap)
var
Microsoft.Win32.SaveFileDialog saveFileDialog =
new Microsoft.Win32.SaveFileDialog();
saveFileDialog.DefaultExt =
"png";
saveFileDialog.Filter =
"PNG Files (*.png)|*.png";
saveFileDialog.FilterIndex = 1;
if (saveFileDialog.ShowDialog() == true
)
I hope it will help you.
Vania
I think that probably works great in Silverlight what to do in just WPF?
Appreciate you help!
I have not tested the code provided by Vania myself, however, I don't recognize anything about it that would indicate it being Silverlight-specific. Have you been able to test it yourself and verify it working in a WPF application?
WriteableBitmap
There didn't seem to be a .Render or .InValidate when trying in a WPF App.
Found a workaround reference on the 'net saying as being available in Silverlight and not WPF. The article said to use RenderTargetBitmap instead.
Then again my translation of that and the code from C# to vb.net could perhaps be a problem also.
....
private void btnSave_Click(object sender, RoutedEventArgs e)
var ms = new MemoryStream();
Microsoft.Win32.
SaveFileDialog saveFileDialog =
if (saveFileDialog.ShowDialog() == true)
using (Stream stream = saveFileDialog.OpenFile())
StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);
sw.Write(ms.ToString().ToCharArray());
sw.Close();
stream.Close();
This code works fine in WPF. I use it in my examples to save barcode image on the disc.
Thanks Vania that looks much more like something I can convert to vb. Appreciate your help.