如何将位图保存到PNG文件而不会导致错误



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

GDI 。

中发生了通用错误

heatmap_PictureBox_Bitmap包含颜色图像。

这可以在Windows 10上的.NET应用程序上显示图片:

public const  MOBILE_UPLOADER_PATH = "C:demoDEMO4MOBILE 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中的目录存在。

问题是目标文件夹public const mobile_uploader_path =" c: demo demo4 mobile uploader "..不存在。

我以为它存在。

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

相关内容

最新更新