【问题标题】:How to save Bitmap to PNG file without causing error如何将位图保存到 PNG 文件而不会导致错误
【发布时间】:2019-02-27 22:16:38
【问题描述】:

我想将位图编码为 PNG,然后将其保存到文件中。 但我的heatmap_PictureBox_Bitmap.Save( filepathname ) ..导致错误:

GDI+ 中出现一般错误。

heatmap_PictureBox_Bitmap 包含彩色图像。

这可以在我的 Windows 10 上的 .NET 应用上正常显示图片:

public const  MOBILE_UPLOADER_PATH = "C:\demo\DEMO4\MOBILE UPLOADER\"
public const XY_HEATMAP_PNG_FILENAME = "XY_HEATMAP.PNG"
public const  XY_HEATMAP_PNG_PATHNAME = MOBILE_UPLOADER_PATH     & XY_HEATMAP_PNG_FILENAME  

dim heatmap_PictureBox_Bitmap   as Bitmap
. . .

' Build the image bitmap:
for x = 0 to x_range
  for y = 0 to y_range
    ' red, green, blue are integers 0 to 255
    pixel_color = Color.FromArgb( red, green, blue )
    heatmap_PictureBox_Bitmap.SetPixel( x, y, pixel_color   )  
  next
next
. . .
PictureBox_heat_map.Image = heatmap_PictureBox_Bitmap  ' displays ok

但添加此行(如下)会导致错误“GDI+ 中发生一般错误。”

. . .
heatmap_PictureBox_Bitmap.Save( XY_HEATMAP_PNG_PATHNAME  ) <<<< CAUSES THE ERROR
PictureBox_heat_map.Image = heatmap_PictureBox_Bitmap

我的假设:
1.heatmap_PictureBox_Bitmap是非编码/压缩图像数据
2.bitmap.save( pathname )编码为.PNG

我也试过

heatmap_PictureBox_Bitmap.Save(XY_HEATMAP_PNG_PATHNAME, System.Drawing.Imaging.ImageFormat.Png)

..同样的问题。

XY_HEATMAP_PNG_PATHNAME 中的目录存在。

【问题讨论】:

  • 如何加载它?加载图像时需要遵循一些规则,例如不释放它们所来自的流。另外,请发布一些实际代码,而不仅仅是伪代码。我们无法从您的代码近似中看出哪里出了问题。
  • 我刚刚添加了有关如何创建位图的代码
  • 对不起,使用给定的代码,我根本没有收到任何错误。你需要提供更多细节。
  • VB 编辑器修复了大小写和间距。这不可能是真正的代码。您是如何创建或加载位图的?
  • 我在 VS 编辑器中关闭了大小写和间距。这是真正的代码。我会提供更多细节。

标签: vb.net image-processing bitmap gdi+


【解决方案1】:

问题是目标文件夹 公共常量 MOBILE_UPLOADER_PATH = "C:\demo\DEMO4\MOBILE UPLOADER\" ..不存在。

我以为它存在。

如果文件夹不存在,我修改了代码来创建文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多