我在使用下面的代码保持我创建的图像的透明度时遇到问题。组合图像始终是黑色而不是透明。这两个图像都是jpg。我看过其他类似的问题,但我无法找到解决问题的方法。
public static string CombineImages(Image[] images, string directoryToSave, string imageName)
{
for (int i = 0; i < SpecialCharsToRemove.Length; i++)
{
while (imageName.Contains(SpecialCharsToRemove[i])) { imageName = imageName.Replace(SpecialCharsToRemove[i], ""); }
}
string finalImage = directoryToSave + "\\" + imageName;
int height = 0;
int width = 0;
for(int i = 0; i < images.Length; i++)
{
if (height < images[i].Height) { height = images[i].Height; }
width += images[i].Width;
}
using (Bitmap image = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(Color.Transparent);
width = 0;
for (int i = 0; i < images.Length; i++)
{
g.DrawImage(images[i], new Point(width, 0));
width += images[i].Width;
}
image.MakeTransparent(image.GetPixel(1, 1));
image.Save(finalImage, ImageFormat.Jpeg);
}
}
return finalImage;
}