Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中使用图像控件显示System.Drawing.Bitmap_C#_Wpf - Fatal编程技术网

C# 在WPF中使用图像控件显示System.Drawing.Bitmap

C# 在WPF中使用图像控件显示System.Drawing.Bitmap,c#,wpf,C#,Wpf,如何将内存中的位图对象分配给WPF中的图像控件?根据 它获取System.Drawing.Bitmap(来自基于Windows的)并将其转换为BitmapSource,它实际上可以用作WPF中图像控件的图像源 image1.Source = YourUtilClass.loadBitmap(SomeBitmap); 可以使用图像的源属性。请尝试此代码 ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));

如何将内存中的
位图
对象分配给WPF中的
图像
控件?

根据

它获取System.Drawing.Bitmap(来自基于Windows的)并将其转换为BitmapSource,它实际上可以用作WPF中图像控件的图像源

image1.Source = YourUtilClass.loadBitmap(SomeBitmap);

可以使用图像的源属性。请尝试此代码

ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));

image1.Source = imageSource;

对于磁盘文件来说很容易,但是对于内存中的位图来说很难

System.Drawing.Bitmap bmp;
Image image;
...
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();

image.Source = bi;

我用
wpf
编写了一个程序,并使用数据库显示图像,这是我的代码:

SqlConnection con = new SqlConnection(@"Data Source=HITMAN-PC\MYSQL;
                                      Initial Catalog=Payam;
                                      Integrated Security=True");

SqlDataAdapter da = new SqlDataAdapter("select * from news", con);

DataTable dt = new DataTable();
da.Fill(dt);

string adress = dt.Rows[i]["ImgLink"].ToString();
ImageSource imgsr = new BitmapImage(new Uri(adress));
PnlImg.Source = imgsr;

我有一个位图对象,实际上它是从扫描设备生成的,所以我不能引用任何位置thx Lars,但我做的更简单,BitmapImage bmpi=new BitmapImage();bmpi.BeginInit();bmpi.StreamSource=新内存流(ByteArray);bmpi.EndInit();图1.来源=bmpi;伟大的您可以将您的解决方案添加为您自己问题的答案。我没有看到BitmapImage.StreamSource方法。Prasand,您是否键入了错误的东西?还是一个属性,当使用非托管句柄(例如HbMMAP)时,请考虑使用安全句柄,请查看确切的副本,但我的答案不泄漏HbMaPiTo这回答了您的问题吗?回答很好,但我强烈建议将Sql对象包装在using语句中,以便在您使用完Sql对象后将其释放。Thx,但代码尚未关闭ms。我认为您将使用@lindexi,即使
MemoryStream
实现
IDisposable
,它不需要显式释放,因为它不包装任何非托管资源。它就像一个字节数组,最终将由GC收集。
SqlConnection con = new SqlConnection(@"Data Source=HITMAN-PC\MYSQL;
                                      Initial Catalog=Payam;
                                      Integrated Security=True");

SqlDataAdapter da = new SqlDataAdapter("select * from news", con);

DataTable dt = new DataTable();
da.Fill(dt);

string adress = dt.Rows[i]["ImgLink"].ToString();
ImageSource imgsr = new BitmapImage(new Uri(adress));
PnlImg.Source = imgsr;