C# Alpha通道透明度和调整图像文件大小
我正在使用以下代码调整tif的大小。tif具有透明度的alpha通道集。我试图调整这张图片的大小,尊重它的透明度,但目前它的背景是黑色的。有什么想法吗C# Alpha通道透明度和调整图像文件大小,c#,image,image-processing,gdi+,C#,Image,Image Processing,Gdi+,我正在使用以下代码调整tif的大小。tif具有透明度的alpha通道集。我试图调整这张图片的大小,尊重它的透明度,但目前它的背景是黑色的。有什么想法吗 public static void ResizeImage(string OriginalImagePath, string NewImagePath, int Width, int Height) { Size NewSize = new Size(Width, Height);
public static void ResizeImage(string OriginalImagePath, string NewImagePath, int Width, int Height)
{
Size NewSize = new Size(Width, Height);
using (Image OriginalImage = Image.FromFile(OriginalImagePath))
{
//Graphics objects can not be created from bitmaps with an Indexed Pixel Format, use RGB instead.
PixelFormat Format = OriginalImage.PixelFormat;
if (Format.ToString().Contains("Indexed"))
Format = PixelFormat.Format24bppRgb;
using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, OriginalImage.PixelFormat))
{
using (Graphics Canvas = Graphics.FromImage(NewImage))
{
Canvas.SmoothingMode = SmoothingMode.AntiAlias;
Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
Canvas.DrawImage(OriginalImage, new Rectangle(new Point(0, 0), NewSize));
NewImage.Save(NewImagePath, OriginalImage.RawFormat);
}
}
}
}
}
试试这个:
if (Format.ToString().Contains("Indexed"))
Format = PixelFormat.Format32bppArgb;
Format32bppArgb
指定像素格式的alpha通道
我想你是有意这么做的:
using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, Format))
编辑:
实际上,尝试将NewImage
上的像素格式强制为Format32bppArgb
如下:
using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height,
PixelFormat.Format32bppArgb))
试试这个:
if (Format.ToString().Contains("Indexed"))
Format = PixelFormat.Format32bppArgb;
Format32bppArgb
指定像素格式的alpha通道
我想你是有意这么做的:
using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, Format))
编辑:
实际上,尝试将NewImage
上的像素格式强制为Format32bppArgb
如下:
using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height,
PixelFormat.Format32bppArgb))
在你离开之前
在你使用blit之前。我实际上发现,由于使用photoshop以tiff格式存储透明度的方式,最好通过自动使用photoshop创建png,然后处理生成的png。我实际上发现,由于使用photoshop以tiff格式存储透明度的方式,最好通过自动化photoshop创建png,然后处理生成的png。尝试了您建议的更改,黑色背景仍在使用。尝试了您建议的更改,黑色背景仍在使用。您是否按照Codesleuth的建议将新位图的像素格式设置为Format32bppRgb?您无法使用24位颜色获得透明度,没有alpha通道。您是否按照Codesleuth的建议将新位图的像素格式设置为Format32bppRgb?使用24位颜色无法获得透明度,没有alpha通道。1)对于遇到问题的图像,OriginalImage.PixelFormat的值是多少?2) 尝试保存为PNG格式。这还会给你黑色像素吗?1)对于你遇到问题的图像,OriginalImage.PixelFormat的值是多少?2) 尝试保存为PNG格式。那还能给你黑色像素吗?