在 wpf 中将图像附加到 image.source
Append image to image.source in wpf
我正在尝试将图像附加到图像源,但执行代码后图像未显示在我的页面中。
代码:
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;
这是我要附加到 imhPhoto.Source 的 data.img 规范。
你可以这样指定路径。
//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;
如果这能解决您的问题:
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;
解决这个问题后,这个问题的解决方案是:
调用函数获取 BitmapImage 并将其保存在 photo 变量中,如下所示:
BitmapImage photo = byteToImage(byte[] buffer)
此函数将字节转换为位图图像
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;
我正在尝试将图像附加到图像源,但执行代码后图像未显示在我的页面中。
代码:
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;
这是我要附加到 imhPhoto.Source 的 data.img 规范。
你可以这样指定路径。
//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;
如果这能解决您的问题:
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;
解决这个问题后,这个问题的解决方案是:
调用函数获取 BitmapImage 并将其保存在 photo 变量中,如下所示:
BitmapImage photo = byteToImage(byte[] buffer)
此函数将字节转换为位图图像
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;