我正在尝试将图像追加到图像源,但是在执行代码后,图像不会显示在我的页面中。
代码:
Bitmap bmp = (Bitmap)data.img;
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();
imgPhoto.Source = bi;这是我想要附加到data.img的imhPhoto.Source规范。

发布于 2016-11-13 05:27:33
在研究了这个问题之后,解决这个问题的办法是:
调用函数获取BitmapImage并将其保存在照片变量中,如下所示:
BitmapImage photo = byteToImage(byte[] buffer)此功能可将字节转换为BitmapImage。
public BitmapImage byteToImage(byte[] buffer)
{
using(var ms = new MemoryStream(buffer))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
}
return image;
}最后,将转换后的照片附加到图像源,如下所示:
imgPhoto.Source = photo;发布于 2016-11-11 05:59:37
你可以像这样分配路径。
//for App folder path
//var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)+"\\Image\\pic.jpg";
//for machine path
var path = @"C:\Users\User1\Pictures\pic.jpg";
Bitmap bmp = new Bitmap(path);
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();
imgPhoto.Source = bi;发布于 2016-11-11 07:09:04
如果这解决了您的问题:
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(@"g:\screenshot.png");
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
bi.StreamSource = ms;
bi.EndInit();
imgPhoto.Source = bi;https://stackoverflow.com/questions/40541363
复制相似问题