C# BitmapImage的FromFile和ToFile等价物是什么?
System.Drawing.Image具有用于FromFile和ToFile的易于使用的方法。Silverlight BitmapImage的等价物是什么?我正在尝试加载并保存jpeg图像作为单元测试的一部分。字节必须与完全匹配才能通过。以下是我目前的猜测:C# BitmapImage的FromFile和ToFile等价物是什么?,c#,wpf,silverlight,C#,Wpf,Silverlight,System.Drawing.Image具有用于FromFile和ToFile的易于使用的方法。Silverlight BitmapImage的等价物是什么?我正在尝试加载并保存jpeg图像作为单元测试的一部分。字节必须与完全匹配才能通过。以下是我目前的猜测: //I am not sure this is right private BitmapImage GetImage(string fileName) { BitmapImage bitmapIma
//I am not sure this is right
private BitmapImage GetImage(string fileName)
{
BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage();
using (Stream imageStreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
bitmapImage.BeginInit();
bitmapImage.StreamSource = imageStreamSource;
bitmapImage.EndInit();
}
return bitmapImage;
}
private void SaveImage(BitmapImage bitmapImage, string file)
{
//How to do this?
}
从MSDN中,使用接受URI或
BitmapImage myBitmapImage = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;
要将图像保存到磁盘,您需要特定的编码器,例如浏览器中的Silverlight应用程序无法使用文件名打开文件。要做到这一点,您需要使用信任度较高的浏览器 Silverlight没有内置图像编码,因此您无法获取位图的内容(顺便说一句,您需要使用
WriteableBitmap
才能访问原始图像)
你从中找到你需要的东西 来自msdn代码段中的注释(第一个链接)要节省大量应用程序内存,请将图像源BitmapImage值的DecodePixelWidth或DecodePixelHeight设置为渲染图像的所需高度和宽度。如果不这样做,应用程序将缓存图像,就好像它是按正常大小渲染的,而不仅仅是显示的大小。”