如何在wpf中显示图像 [英] How to dispay image in wpf

查看:109
本文介绍了如何在wpf中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好......



我在Bitmap或Byte中存储数据。如何使用此位图或字节显示图像。??

我有使用此功能。



private System.Drawing.Bitmap ImageToBitmap( byte [] colorData,int imageWidth,int imageHeight,int imagePixelDataLength)

{

byte [] pixeldata = colorData;

System.Drawing。位图bmap = new System.Drawing.Bitmap(imageWidth,imageHeight,System.Drawing.Imaging.PixelFormat.Format32bppRgb);

System.Drawing.Imaging.BitmapData bmapdata = bmap.LockBits(

new System.Drawing.Rectangle(0,0,imageWidth,imageHeight),

System.Drawing.Imaging.ImageLockMode.WriteOnly,

bmap.PixelFormat) ;

IntPtr ptr = bmapdata.Scan0;



byte [] byteArray = new byte [0];

使用(MemoryStream stream = new MemoryStream())

{

bmap.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

stream.Close();

byteArray = stream.ToArray();

//在此显示图像或其他

}

//img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr,IntPtr.Zero ,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());



Marshal.Copy(pixeldata,0,ptr,imagePixelDataLength);

bmap .UnlockBits(bmapdata);

返回bmap;

}

Hello...

I have store data in Bitmap or Byte. How to display image using this Bitmap or Byte.??
I have Use this function.

private System.Drawing.Bitmap ImageToBitmap(byte[] colorData, int imageWidth, int imageHeight, int imagePixelDataLength)
{
byte[] pixeldata = colorData;
System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
System.Drawing.Imaging.BitmapData bmapdata = bmap.LockBits(
new System.Drawing.Rectangle(0, 0, imageWidth, imageHeight),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;

byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
bmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Close();
byteArray = stream.ToArray();
// Display Image Here or Other
}
//img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

Marshal.Copy(pixeldata, 0, ptr, imagePixelDataLength);
bmap.UnlockBits(bmapdata);
return bmap;
}

推荐答案

为什么要使用 System.Drawing.Bitmap WPF中的图像?也许,根据你的问题的标题,你真的需要在WPF中显示图像?但它不一定是 System.Drawing.Bitmap 。 WPF有自己的图像类型。看看这个类层次结构:

http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource%28v=vs.110%29.aspx#inheritanceContinued [ ^ ]。



有时确实有 System.Drawing.Bitmap 并需要将其转换为show在WPF中,在某些情况下是有意义的。但你不是那样做的。从您的函数,您正在尝试 System.Drawing.Bitmap 的实例(在WPF中使用无用)并将其保存到文件。不明白为什么。 (这是误导功能用户的方法,有一些副作用,在你的情况下保存文件,而不是只返回一个值。)

互操作性在 System.Drawing 和WPF之间,请参阅:

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.interopbitmap%28v=vs.110%29.aspx [ ^ ]。



我不讨论任何问题这个主题的细节,只是因为这不是你想要做的。您正在尝试使用某些数据, colorData 。您绝对不需要在 System.Drawing.Bitmap 中编写它。你需要将它写入一些你可以在WPF中使用的对象,这是一个非常不同的东西。

首先,我不知道你的数据是如何组织的。由您来解决并使用适当的像素表示。您可以使用类 System.Windows.Media.Imaging.WriteableBitmap

请阅读文档:http://msdn.microsoft.com/en-us/library/system .windows.media.imaging.writeablebitmap%28v = vs.110%29.aspx [ ^ ]。



< dd> -SA
Why would you use System.Drawing.Bitmap image in WPF? Maybe, according to the title of your question, you really need "to display image in WPF"? But then it does not have to be System.Drawing.Bitmap. WPF has its own image types. Look at this class hierarchy:
http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource%28v=vs.110%29.aspx#inheritanceContinued[^].

Sometimes really have System.Drawing.Bitmap and need to convert it to show in WPF, which makes sense in certain cases. But you are not doing that. From your function, you are trying to the instance of System.Drawing.Bitmap (which is useless for using in WPF) and save it to file. Not clear why. (And this is the way to mislead the function users, to have some side effect, saving a file in your case, instead of just returning a value.)
For interoperability between System.Drawing and WPF, please see:
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.interopbitmap%28v=vs.110%29.aspx[^].

I'm not discussing any detail on this topic, just because this is hardly what you are trying to do. You are trying to utilize some data, colorData. You absolutely don't need to write it in System.Drawing.Bitmap. You need to write it to some object which you could utilize in WPF, which is a very different thing.
First of all, I don't know how your data is organized. It's up to you to sort it out and use appropriate pixel representation. You can use the class System.Windows.Media.Imaging.WriteableBitmap.
Please read the documentation: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap%28v=vs.110%29.aspx[^].

—SA


您可以通过以下方式显示图像:

You can display image from this way:
ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));

image1.Source = imageSource;



另一种方式:


Another way:

Bitmap bitmap = YourProject.Properties.Resources.YourImage;
object source = new ImageSourceConverter().ConvertFrom(bitmap.GetHbitmap());
ImageSource imageSource = (ImageSource) source;
image1.Icon = imageSource;



这种方式 [ ^ ](来自.resx文件)



网上有很多例子,只需搜索一下。



希望它有所帮助。


Or this way[^] (From .resx file)

There are many examples on the net, just search it.

Hope it helps.


这篇关于如何在wpf中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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