- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Windows 窗体应用程序中遇到问题,当我保存到 MemoryStream 时,Bitmap.Save 失败。这个问题似乎只在一台机器上间歇性地发生(到目前为止),坏消息是在客户现场。我无法在机器上调试,但我得到了一个堆栈跟踪,将问题缩小到一行代码。
这是我的代码的精简版:
byte[] ConvertPixelsToBytes()
{
// imagine a picture class that creates a 24 bbp image, and has
// a method to get an unmanaged pixel buffer.
// imagine it also has methods for getting the width,
// height, pitch
// I suppose this line could return a bad address, but
// I would have expected that the Bitmap constructor would have
// failed if it was
System.IntPtr pPixels = picture.GetPixelData();
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(
picture.width(),
picture.height(),
picture.pitch(),
System.Drawing.Imaging.PixelFormat.Format24bppRgb,
pPixels );
// This line doesn't actually free the memory, but it could be freed in a
// background thread
// (2)
picture.releasePixelData(pPixels);
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
try
{
// I don't see how this line could fail, but it does
// (3)
bmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
return memStream.ToArray();
}
catch(System.Runtime.InteropServices.ExternalException e)
{
// e.Message is the very helpful " A generic error occurred in GDI+."
}
finally
{
memStream.Dispose();
}
return new byte[0];
}
知道会发生什么吗?我很确定我的像素缓冲区是正确的,它始终可以在我们的开发/测试机器和其他客户站点上运行。
我对失败的可能原因的看法是
一个。位图构造函数不复制像素数据,而是保留对像素数据的引用,并且由于释放内存而导致保存失败。我没有发现 MSDN 文档在这一点上很清楚,但我假设位图复制像素数据而不是假设它被锁定。
像素数据无效,导致 Save 方法失败。我对此表示怀疑,因为我的像素数据是每像素 24 位,所以据我所知它应该不会无效。
.NET 框架有问题。
如果您对其他可能的失败原因有任何想法,我将不胜感激,这样我就可以向我的应用程序添加额外的检查和日志记录信息,这样我就可以将一些东西发送到现场。
最佳答案
该位图构造函数的 MSDN 文档毫无疑问:
Remarks The caller is responsible for allocating and freeing the block of memory specified by the scan0 parameter, however, the memory should not be released until the related Bitmap is released.
关于c# - GDI+ 异常将位图保存到 MemoryStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/521927/
我有一个返回 MemoryStream? 的函数。如果发生错误,则为 null。然后发现不能声明变量MemoryStream? public MemoryStream? GetResponseStre
我正在将一个结构序列化到 MemoryStream 中,我想保存和加载序列化的结构。 那么,如何将 MemoryStream 保存到文件中并从文件中加载它呢? 最佳答案 您可以使用MemoryStre
我应该调用哪个? 有必要两者都调用吗? 如果我已经调用了其中一个,另一个会抛出异常吗? 最佳答案 Close() 和 Dispose() 在 MemoryStream 上调用时,仅用于做两件事: 将对
今天我注意到 MemoryStream 有点奇怪类(class)。 .Length属性是一个 long,但是 .Capacity属性,大概应该总是 >= .Length 只是一个 int。 我知道需要
我有这样的方法: public DataObject GetClipboardData() { var result = new DataObject(); result.SetTex
好的,我看过一些文章here和 here ,但他们没有做我需要做的事情,我遇到了一些麻烦。 我正在接收一段作为内存流的加密数据。我需要以某种方式将内存流写入文件(我编写模型的方式,最好是字符串),然后
我确实看到了“PDF to Image using GhostScript. No image file has to be created”,但这只是(某种程度上)回答了我一半的问题。是否可以使用
这个问题在这里已经有了答案: Stream.Seek(0, SeekOrigin.Begin) or Position = 0 (3 个答案) 关闭 9 年前。 使用的优点或缺点是什么(或区别) m
我在一些开源代码中发现了以下结构: var mstream = new MemoryStream(); // ... write some data to mstream mstream.Close(
由于 MemoryStream 是非托管资源,它是否总是需要被处理? 鉴于: 1) A method is invoked. 2) A MemoryStream object is created (
我正在尝试使用 dotMemory 进行一个简单的测试。我希望以下测试能够通过: [Test] public void MemoryStream_is_disposed() { using (
多次使用 MemoryStream 时遇到问题。 例子: For Each XImage As XImage In pdfDocument.Pages(pageCount).Resources.Ima
在使用 MemoryStream 时,我发现自己经常将数据复制(因此复制)到临时字节数组。 我认为这有点浪费资源,因为 MemoryStream 不允许您直接访问底层字节数组。 在这种情况下,Memo
如何在 Delphi 中将“Hello World”字符串、clrf 和一些随机 10 个字节写入内存流? 最佳答案 我会考虑使用二进制编写器来完成此任务。这是一个更高级别的类,负责处理将数据获取到流
我正在使用 MemoryStream 构造一个需要发送到服务器的字节数组。我有三个问题: 1) 有没有比这更好的方法来构造字节数组? 2)为什么这段代码会在我的内存流中写入虚假内容? var se
这可能是一个非常简单的问题,我认为我所追求的只是声明新 MemoryStream 的最佳实践 以下2个样本有什么区别: MemoryStream myStream = new MemoryStream
我正在尝试压缩 XML 树并将其用作电子邮件附件。带附件的电子邮件发送成功,但创建的 zip 文件始终损坏 - 它不是有效的 zip 文件,但包含二进制数据。 问题重现如下,具体参见BuildAtta
使用sharpPDF库,我生成了一个pdf内存流,并且我想直接通过电子邮件发送它。但是 ms.Seek(.... 行给出了一个 ObjectDisposeException; Cannot acces
我有一台 2 GB 的机器。在运行我的 exe 之前,我有 1.1 GB 的可用内存。当我运行下面只有一行的 exe 时,出现内存不足异常。 我预计以下行将使用大约 6 亿字节。 MemoryStre
当您从字节数组构造 MemoryStream 时,该流是否会创建自己的副本?如果,与直接从原始数组中读取字节相比,这是否是高性能场景中的性能问题? 最佳答案 它似乎没有在文档中明确提及,但请记住,您可
我是一名优秀的程序员,十分优秀!