Brad Barnhill (http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library)의 Barcode Image Generation Libary
을 사용하여 바코드 이미지를 만드는 광산 앱을 다시 작성합니다.이미지가 Wpf Image Control에 표시되지 않습니다.
이 기사에서는 Windows Forms에서 모든 작업을 수행하는 방법에 대해 설명합니다. 하지만 지금은 - 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;
}
이미지를 변환 (및 이미지 컨트롤에 할당)
clemens! 코드를 테스트 한 후 XAML을 엉망으로 만들었습니다. 이미지 컨트롤은 외부 stackpanel-border mix로 shadowd되었습니다. 하지만 코드는 잘 작동합니다. D – Radinator