WPF中的图片框等效 [英] Picture box eqivalent in WPF

查看:100
本文介绍了WPF中的图片框等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在使用第三方dll在我的应用程序中生成条形码。为此,我使用的代码是

Hello,

I am using a third party dll for generating barcode in my application. For that the code I am using is

Zen.Barcode.Code39BarcodeDraw obj = Zen.Barcode.BarcodeDrawFactory.Code39WithoutChecksum;
picturebox1.image = obj.draw("123456789",100);



完美地在Win Forms中工作。但是当我尝试在WPF中使用相同的dll而不是picturebox时我的选项是Image



但是我无法将条形码图像绘制到WPF图像控件。(有没有财产来设置图片)



请提供宝贵的建议。


its perfectly working in Win Forms. But when I try to use the same dll in WPF instead of picturebox my option available is Image

But I cannot draw the barcode image to WPF image control.(There is no property to set image)

Please give your valuable suggestions.

推荐答案

你不需要这个等价物,因为即使在 System.Windows.Forms 中也不需要它。这仅仅是一个控制包装代码渲染一些图像。与基于原始Windows API和Windows类的其他控件不同,您可以自己完成。



在API中,不使用原始窗口类(主窗口除外)。在WPF中显示任何图像都没有问题(请参阅所有与此相关的内容: http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource%28v=vs.110%29.aspx [ ^ ]),但如果您的库仅使用 System.Drawing.Image ,则它无法与WPF图像一起使用。一种选择是:继续使用 System.Drawing.Image (它与 System.Windows.Forms 无关),但您需要将结果转换为WPF图像。有几种方法可以做到这一点:

You don't need this equivalent, because it is not really needed even in System.Windows.Forms. This is merely a control wrapping code rendering some image. Unlike other control based on raw Windows API and windows classes, you could do it by yourself.

In API, raw windows classes are not used (except main window). There is not a problem to show any images in WPF (please see all related to this: http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource%28v=vs.110%29.aspx[^]), but if your library works with System.Drawing.Image only, it cannot work with WPF images. One option is this: keep using System.Drawing.Image (it has nothing to do with System.Windows.Forms), but you will need to convert the result to WPF image. There are a couple of ways to do that:
  • You can save System.Drawing.Bitmap (or any other image) to memory stream and read it back in WPF.
  • Use System.Windows.Interop.Imagining:
    http://msdn.microsoft.com/en-us/library/System.Windows.Interop.Imaging%28v=vs.110%29.aspx[^],
    http://msdn.microsoft.com/en-us/library/system.windows.interop.imaging.createbitmapsourcefromhbitmap%28v=vs.110%29.aspx[^].


我刚才有同样的使用图像控件(名为imgMfgNameBarcode),让它像这样工作。仍然测试它但似乎工作。



I just had the same problem and got it to work like this, using the image control (named imgMfgNameBarcode). Still testing it but seems to work.

using Zen.Barcode;

Code39BarcodeDraw barcode39 = BarcodeDrawFactory.Code39WithoutChecksum;

lblMfgName.Content = "BinOptics Corporation";

//Image we want to get the WPF Image from...
sTest = lblMfgName.Content.ToString();
System.Drawing.Image img =
barcode39.Draw(lblMfgName.Content.ToString(), 40, 2);

// ImageSource ...
BitmapImage bi = new BitmapImage();

bi.BeginInit();

MemoryStream ms = new MemoryStream();

// Save to a memory stream...
img.Save(ms, ImageFormat.Bmp);

// Rewind the stream...
ms.Seek(0, SeekOrigin.Begin);

// Tell the WPF image to use this stream...
bi.StreamSource = ms;

bi.EndInit();

imgMfgNameBarcode.Source = bi;


这篇关于WPF中的图片框等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆