- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试使用 BinaryFormatter 序列化一些图像时,我会得到一个 ExternalException - GDI+ 中发生一般性错误。”摸索了一会儿之后,我决定创建一个简单的测试缩小问题范围的项目:
static void Main(string[] args)
{
string file = @"C:\temp\delme.jpg";
//Image i = new Bitmap(file);
//using(FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
byte[] data = File.ReadAllBytes(file);
using(MemoryStream originalms = new MemoryStream(data))
{
using (Image i = Image.FromStream(originalms))
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
// Throws ExternalException on Windows 7, not Windows XP
bf.Serialize(ms, i);
}
}
}
}
对于特定的图像,我尝试了各种加载图像的方法,但我无法让它在 Windows 7 下工作,即使以管理员身份运行该程序也是如此。
我已经将完全相同的可执行文件和镜像复制到我的 Windows XP VMWare 实例中,我没有遇到任何问题。
有人知道为什么某些图像在 Windows 7 下不起作用,但在 XP 下可以吗?
这是其中一张图片: http://www.2shared.com/file/7wAXL88i/SO_testimage.html
delme.jpg md5: 3d7e832db108de35400edc28142a8281
最佳答案
正如 OP 所指出的,所提供的代码引发了一个异常,该异常似乎只发生在他提供的图像上,但与我机器上的其他图像一起工作正常。
选项 1
static void Main(string[] args)
{
string file = @"C:\Users\Public\Pictures\delme.jpg";
byte[] data = File.ReadAllBytes(file);
using (MemoryStream originalms = new MemoryStream(data))
{
using (Image i = Image.FromStream(originalms))
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
// Throws ExternalException on Windows 7, not Windows XP
//bf.Serialize(ms, i);
i.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); // Works
i.Save(ms, System.Drawing.Imaging.ImageFormat.Png); // Works
i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // Fails
}
}
}
}
可能是有问题的图像是使用添加了一些干扰 JPEG 序列化的额外信息的工具创建的。
附言图像可以使用BMP 或PNG 格式保存到内存流中。如果可以选择更改格式,那么您可以尝试使用这些格式或 ImageFormat 中定义的任何其他格式。
选项 2如果您的目标只是将图像文件的内容放入内存流中,那么执行以下操作会有所帮助
static void Main(string[] args)
{
string file = @"C:\Users\Public\Pictures\delme.jpg";
using (FileStream fileStream = File.OpenRead(file))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
}
}
关于c# - BinaryFormatter.Serialize( Image ) - ExternalException - GDI+ 中发生一般错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15916661/
这两个库之间有什么区别吗? 最佳答案 根据the wikipedia article on GDI : With the introduction of Windows XP, GDI was dep
我喜欢 GDI+,因为它具有高性能并且包含在 Windows XP 中。但是,它的模糊类和效果类仅在 GDI+ 1.1 中可用,后者仅随 Windows Vista 或更高版本提供。尽管微软计划很快放
我用 C# 创建了一个图像服务,它采用基础层图像 (JPG),再分层一层更透明的 PNG(32 位),然后输出最终的 JPG 图像。我试图从这个函数中挤出最后一毫秒,但我的代码在 GDI+ 中的 Dr
我正在使用顶点以及 LineTo() 和 MoveTo() 函数制作一系列三角形来表示 3D 形状。 着色/填充这些三角形的最佳方法是什么? 谢谢 最佳答案 MSDN has a sample tha
我在 Win7 系统上注意到过一个问题,以为是 DWM 错误,但在重新启动后已修复。但现在我意识到它在其他人的系统上发生(作为默认行为),并且这也是 Surface Pro 上的正常行为。 如何重现问
是否可以在用户模式下创建自定义 GDI 设备?我们的想法是创建一个设备上下文 (HDC),我们可以将其传递给不透明的组件,这样当组件调用 GDI 函数(如 TextOut)时,我们可以获得实际的文本字
我想知道是否可以枚举所有桌面可见窗口以创建它们的位图并将所有位图组合成一个以便获得桌面的完整屏幕截图? 最有可能使用 GDI/GD+,但也欢迎使用任何外部库。首选语言C\C++。 提前致谢。 最佳答案
我有一个生成元文件 (EMF) 的应用程序。它使用引用设备(又名屏幕)来呈现这些元文件,因此元文件的 DPI 会根据代码运行的机器而变化。 假设我的代码打算创建一个 8.5 x 11 英寸的元文件。
我在同时使用 GDI 和 GDI+ 进行绘图时遇到问题。页面转换——尤其是缩放——似乎在两者之间有点偏离。除了 SetViewportExt 和 SetWindowExt 之外,GDI 上下文的哪些属
我需要从应用程序中获取一个大表(300K+ 行)并且没有导出功能。经过多次不成功的尝试后,我只剩下一个复制粘贴宏,一次一行。如果有办法在绘制时获取字符串,我可以一次获取一页(40 行)。 最佳答案 如
我曾经在我的 Win7 系统上注意到一个问题,认为这是一个 DWM 错误,因为它在重新启动后得到修复。但现在我意识到它正在其他人的系统上发生(作为默认行为),这也是 Surface Pro 上的正常行
我正在寻找一些高级教程,或者可能是用 C++ 或 .NET 编写的开源应用程序,它们可以实现复杂的基于 vector 的应用程序,例如 MS Visio 或 Autocad。我需要知道的是,当用户可以
我正在使用第三方库来绘制图表。该库需要一个 HDC (GDI)。但是我的应用程序使用的是 GDI+。 Q1:这样做安全吗? void f(Gdiplus::Graphics& graphics) {
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
对不起,如果这是题外话。如果是这样的话;请随时将其移至适当的站点。 GDI/GDI+ 如何在不使用 DirectX 或 OpenGL 等较低级别的 API 与 GPU 通信的情况下渲染到显卡(在屏幕上
我正在尝试在兼容的渲染目标上使用 Gdi 和 Direct 2D 来渲染位图。我使用 D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_GDI_COMPATIBLE 选项创建
我们目前正在将一个较旧的应用程序转换为通过 GDI+ 绘制,而不是直接使用 GDI。随着我们逐步翻译系统,有时我们需要从 Gdiplus::Graphics 对象中获取 HDC,以便允许尚未翻译的代码
我正在尝试使用 GDI+ 在屏幕上(整个屏幕,在所有其他窗口的顶部)绘图。 我已将 NULL 传递给 GetDC 以将 HDC 显示到屏幕上,然后使用它创建一个 Graphics 对象,并使用 Dra
GDI+ 非常慢,几乎完全是软件,而 GDI 是高度硬件加速的。GDI+ 是 Graphics 类在 WinForms 上使用的东西,它太慢了。 有没有人制作了 .NET GDI 库以便我们可以有速度
ASP.NET 网站随机引发此 System.Drawing-error: System.Runtime.InteropServices.ExternalException:System.Drawin
我是一名优秀的程序员,十分优秀!