我有qrcode生成器(我使用ZXing.QrCode)
public Bitmap GenerateQR(int width, int height, string text)
{
var bw = new ZXing.BarcodeWriter();
var encOptions = new ZXing.Common.EncodingOptions() { Width = width, Height = height, Margin = 0 };
bw.Options = encOptions;
bw.Format = ZXing.BarcodeFormat.QR_CODE;
var result = new Bitmap(bw.Write(text));
return result;
}
现在我想在新窗口显示qr代码,所以我打电话给:
var window = new Zeszycik.View.show(GenerateQR(300,300,"some txt"));
window.Show();
但我不知道如何在新窗口中显示qrcode
public show(Bitmap qrcode)
{
InitializeComponent();
print(qrcode);
}
private void print(Bitmap img)
{
image.Source = img; //error
}
答案 0 :(得分:0)
图片来源属性的类型为 ImageSource
,但 Bitmap
并非来自ImageSource,因此您需要将其转换为实例ImageSource可以是 BitmapImage
或 BitmapSource
。
现在,要将Bitmap转换为BitmapSource,您可以参考此链接here。为了完整答案,我将在此处的链接中发布答案:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
private BitmapSource Bitmap2BitmapSource(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitMap();
BitmapSource retval;
try
{
retval = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(hBitmap); //Necessary to avoid memory leak.
}
return retval;
}
现在您可以像这样设置图像源:
image.Source = Bitmap2BitmapSource(img);