在WPF和C#的工作,我有一个TransformedBitmap对象,我可以:将TransformedBitmap对象保存到磁盘。
- 需要保存到磁盘作为位图文件类型(理想情况下,我会允许用户选择是否它保存为BMP ,JPG,TIF等,但我还没有到那个阶段呢......)
- 需要转换为BitmapImage对象,因为我知道如何从BitmapImage对象获取byte []。
不幸的是,在这一点上,我非常努力地完成这两件事中的任何一件。
任何人都可以提供任何帮助或指出我可能会失踪的任何方法吗?
在WPF和C#的工作,我有一个TransformedBitmap对象,我可以:将TransformedBitmap对象保存到磁盘。
不幸的是,在这一点上,我非常努力地完成这两件事中的任何一件。
任何人都可以提供任何帮助或指出我可能会失踪的任何方法吗?
所有的编码器都使用BitmapFrame
类来创建帧,这些帧将被添加到编码器的Frames
集合属性中。方法有多种过载方法,其中一种接受BitmapSource
类型的参数。所以我们知道TransformedBitmap
继承自BitmapSource
我们可以将它作为参数传递给BitmapFrame.Create
方法。以下是为你描述其运作方式:
public bool WriteTransformedBitmapToFile<T>(BitmapSource bitmapSource, string fileName) where T : BitmapEncoder, new()
{
if (string.IsNullOrEmpty(fileName) || bitmapSource == null)
return false;
//creating frame and putting it to Frames collection of selected encoder
var frame = BitmapFrame.Create(bitmapSource);
var encoder = new T();
encoder.Frames.Add(frame);
try
{
using (var fs = new FileStream(fileName, FileMode.Create))
{
encoder.Save(fs);
}
}
catch (Exception e)
{
return false;
}
return true;
}
private BitmapImage GetBitmapImage<T>(BitmapSource bitmapSource) where T : BitmapEncoder, new()
{
var frame = BitmapFrame.Create(bitmapSource);
var encoder = new T();
encoder.Frames.Add(frame);
var bitmapImage = new BitmapImage();
bool isCreated;
try
{
using (var ms = new MemoryStream())
{
encoder.Save(ms);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
isCreated = true;
}
}
catch
{
isCreated = false;
}
return isCreated ? bitmapImage : null;
}
他们接受任何的BitmapSource作为第一个参数和任何的BitmapEncoder作为泛型类型参数。
希望这会有所帮助。
哇,非常非常!我真的不知道不同的编码器是如何工作的(或者如何应用它们)。万分感谢! – JToland 2010-09-07 15:20:08
发布链接并不是真正的答案,但我认为这可能是一个好的开始。 http://msdn.microsoft.com/en-us/library/ms750864.aspx – Val 2010-09-07 13:54:34