我正在重新编写我的应用程序,在那里我使用Brad Barnhill的Barcode Image Generation Libary
(http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library)创建条形码图像。图像不会显示在Wpf图像控制
在本文中,所有内容都被解释为如何在Windows窗体中执行此操作。但现在 - 使用Wpf - 有一些错误。例如:函数Encode
的结果返回System.Drawing.Image
,但是当我想要在Wpf Image Control
中显示此图像时,Source
属性需要System.Windows.Media.ImageSource
。
因此,我做了一些关于如何将Drawing.Image
转换为Media.ImageSource
的研究。我发现了一些片段,但他们没有按预期工作。
目前我使用此代码:
// Import:
using Media = System.Windows.Media;
using Forms = System.Windows.Forms;
// Setting some porperties of the barcode-object
this.barcode.RotateFlipType = this.bcvm.Rotation.Rotation;
this.barcode.Alignment = this.bcvm.Ausrichtung.Alignment;
this.barcode.LabelPosition = this.bcvm.Position.Position;
// this.bcvm is my BarcodeViewModel for MVVM
var img = this.barcode.Encode(
this.bcvm.Encoding.Encoding,
this.bcvm.EingabeWert,
this.bcvm.ForeColor.ToDrawingColor(),
this.bcvm.BackColor.ToDrawingColor(),
(int)this.bcvm.Breite,
(int)this.bcvm.Hoehe
);
this.imgBarcode.Source = img.DrawingImageToWpfImage();
this.imgBarcode.Width = img.Width;
this.imgBarcode.Height = img.Height;
// My conversion methode. It takes a Drawing.Image and returns a Media.ImageSource
public static Media.ImageSource ToImageSource(this Drawing.Image drawingImage)
{
Media.ImageSource imgSrc = new Media.Imaging.BitmapImage();
using (MemoryStream ms = new MemoryStream())
{
drawingImage.Save(ms, Drawing.Imaging.ImageFormat.Png);
(imgSrc as Media.Imaging.BitmapImage).BeginInit();
(imgSrc as Media.Imaging.BitmapImage).StreamSource = new MemoryStream(ms.ToArray());
(imgSrc as Media.Imaging.BitmapImage).EndInit();
}
return imgSrc;
}
当运行该代码的转换图像(并将其分配给所述图片控制)没有什么显示
谢谢你克莱门斯!在尝试你的代码后,我注意到我只是搞乱了我的XAML。图像控制由外部堆叠面板边界混合组成。但你的代码也可以工作:D – Radinator