如 原始图片对象定义为 destImage ,如果 destImage 为 索引像素格式/索引色格式(泛指)
一般适用于 例如:网站图片、限定图片大小的程序(常见的gif、png,或是jpg的 黑白图片)
即: destImage.PixelFormat 为以下几种(索引格式的、不明确的、指定色调范围的)
索引格式:Format1bppIndexed、Format4bppIndexed、Format8bppIndexed
不明确的:Undefined、DontCare
指定色调范围的Format16bppArgb1555、Format16bppGrayScale
参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.imaging.pixelformat?view=net-5.0#System_Drawing_Imaging_PixelFormat_Undefined
那么如使用方法: using (Graphics g = Graphics.FromImage(destImage))
提示:无法从带有索引像素格式(索引色)的图像创建 Graphics 对象
常见处理方式是 将 destImage 复制到另外一个 Bitmap 对象,随后使用 Graphics 即可
注意new 需注意:
错误的:Bitmap bmp = new Bitmap(destWidth, destHeight, destImage.PixelFormat); //这里带了自身的限定格式 做的new
正确的:Bitmap bmp = new Bitmap(destWidth, destHeight);