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

问题描述 投票:-1回答:1

我想将位图编码为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.net image-processing bitmap gdi+
1个回答
0
投票

问题是目标文件夹public const MOBILE_UPLOADER_PATH =“C:\ demo \ DEMO4 \ MOBILE UPLOADER \”..did不存在。

我以为它存在了。

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

© www.soinside.com 2019 - 2024. All rights reserved.