我无法将图像资源转换为字节[]。将资源转换为字节[]
例如,我有以下资源:
pack://application:,,,/AppName;component/Assets/Images/sampleimage.jpg
在我的计划。如何将其转换为字节[]。
我试过使用BitMapImage,但它的ImageSource最终在初始化后变为null。
我无法将图像资源转换为字节[]。将资源转换为字节[]
例如,我有以下资源:
pack://application:,,,/AppName;component/Assets/Images/sampleimage.jpg
在我的计划。如何将其转换为字节[]。
我试过使用BitMapImage,但它的ImageSource最终在初始化后变为null。
这似乎工作:
var info = Application.GetResourceStream(uri);
var memoryStream = new MemoryStream();
info.Stream.CopyTo(memoryStream);
return memoryStream.ToArray();
的一般解为BitmapSource
转换成byte[]
是这样的:
public byte[] GetImageBuffer(BitmapSource bitmap, BitmapEncoder encoder)
{
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
return stream.ToArray();
}
}
你可以使用下面这样想,任何的BitmapEncoder s在WPF中可用。
var uri = new Uri("pack://application:,,,/AppName;component/Assets/Images/sampleimage.jpg");
var bitmap = new BitmapImage(uri);
var buffer = GetImageBuffer(bitmap, new JpegBitmapEncoder());
你有没有读过[这个问题](http://stackoverflow.com/questions/1192054/load-image-from-resources-in-c-sharp)关于图像资源? – Corey 2013-02-18 05:04:11