C#位映射到位图转换出内存错误
我目前正在尝试从bitmapimage转换一个大图像 - >位图。在较小的文件上,转换效果很好,该问题源于尝试转换IS LT; 5MB的图像。进行了一些阅读后,我认为我应该使用某种缓冲区来填充位图,但不确定。正确方向的任何点都会有所帮助。谢谢!
抛出的错误消息:
'outStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
/// <summary>
/// Convert a BitmapImage found in a Image.Source to a Bitmap
/// </summary>
/// <param name="bitmapImage"></param>
/// <returns></returns>
public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
// Opens a stream to be used for the image conversion
using (MemoryStream outStream = new MemoryStream())
{
// Grabs the encoder object to be used as a converter
BitmapEncoder encoder = new BmpBitmapEncoder();
// Adds the bitmap as a frame of the encoder (this adds the image as a 'page' for the bitmap image)
BitmapFrame frame = BitmapFrame.Create(bitmapImage);
encoder.Frames.Add(frame);
// Saves the current set of frames (single page for now) to the stream
encoder.Save(outStream);
encoder.Frames.Clear();
encoder = null;
frame = null;
// Initialize a new Bitmap image using the stream
return new Bitmap(outStream); <--- This is where the error is thrown
}
}
I'm currently trying to convert a large image from BitmapImage -> Bitmap. On smaller files the conversion works great, the issue stems from trying to convert an image that is <5MB. After doing some reading I think I should be using some sort of buffer to populate the bitmap but not sure. Any point in the right direction would be helpful. Thanks!
Error message thrown:
'outStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
/// <summary>
/// Convert a BitmapImage found in a Image.Source to a Bitmap
/// </summary>
/// <param name="bitmapImage"></param>
/// <returns></returns>
public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
// Opens a stream to be used for the image conversion
using (MemoryStream outStream = new MemoryStream())
{
// Grabs the encoder object to be used as a converter
BitmapEncoder encoder = new BmpBitmapEncoder();
// Adds the bitmap as a frame of the encoder (this adds the image as a 'page' for the bitmap image)
BitmapFrame frame = BitmapFrame.Create(bitmapImage);
encoder.Frames.Add(frame);
// Saves the current set of frames (single page for now) to the stream
encoder.Save(outStream);
encoder.Frames.Clear();
encoder = null;
frame = null;
// Initialize a new Bitmap image using the stream
return new Bitmap(outStream); <--- This is where the error is thrown
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论