即使位图设置为 Graphics.Clear(Color.Transparent),为什么调整大小时图像周围会出现黑色背景
我正在构建一个图像上传工具,可以调整图像大小以适合固定大小,但它会在图像周围的填充空间中添加黑色背景而不是透明背景。
我读过,位图需要设置为带有 Alpha 层的 PixelFormat,并且我可以将图形清晰颜色设置为透明,但我仍然遇到同样的问题。
我的图像大部分是 jpeg。这是代码:
private void ResizeImage(Image Original, Int32 newWidth, Int32 newHeight, String pathToSave)
{
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int originalWidth = Original.Width;
int originalHeight = Original.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)newWidth / (float)originalWidth);
nPercentH = ((float)newHeight / (float)originalHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(originalWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(originalHeight * nPercent)) / 2);
}
int destWidth = (int)(originalWidth * nPercent);
int destHeight = (int)(originalHeight * nPercent);
Bitmap bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
bmp.SetResolution(Original.HorizontalResolution, Original.VerticalResolution);
using (Graphics Graphic = Graphics.FromImage(bmp))
{
Graphic.CompositingQuality = CompositingQuality.HighQuality;
Graphic.Clear(Color.Red);
Graphic.CompositingMode = CompositingMode.SourceCopy;
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(
Original,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, originalWidth, originalHeight),
GraphicsUnit.Pixel
);
bmp.Save(pathToSave,Original.RawFormat);
}
}
I'm building an Image upload tool that resizes an image to fit a fixed size but it's adding a black background instead of a transparent one to the filler space around the image.
I've read that the Bitmap needs to be set to a PixelFormat with an Alpha layer and that I can set the Graphics clear colour to transparent but I'm still getting the same problem.
My images are mostly jpegs. Here is the code:
private void ResizeImage(Image Original, Int32 newWidth, Int32 newHeight, String pathToSave)
{
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int originalWidth = Original.Width;
int originalHeight = Original.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)newWidth / (float)originalWidth);
nPercentH = ((float)newHeight / (float)originalHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(originalWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(originalHeight * nPercent)) / 2);
}
int destWidth = (int)(originalWidth * nPercent);
int destHeight = (int)(originalHeight * nPercent);
Bitmap bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
bmp.SetResolution(Original.HorizontalResolution, Original.VerticalResolution);
using (Graphics Graphic = Graphics.FromImage(bmp))
{
Graphic.CompositingQuality = CompositingQuality.HighQuality;
Graphic.Clear(Color.Red);
Graphic.CompositingMode = CompositingMode.SourceCopy;
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(
Original,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, originalWidth, originalHeight),
GraphicsUnit.Pixel
);
bmp.Save(pathToSave,Original.RawFormat);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你把背景设为红色,而不是黑色。如果您希望将背景的 alpha 设置为 0,请使用 Color.Transparent。或者直接省略 Clear(),它是新位图的默认值。并避免在 Save() 调用中使用 Original.RawFormat,您不想使用不支持透明度的图像格式。 Png总是好的。并确保无论您使用什么方法显示生成的位图也支持透明度。具有明确的背景颜色。如果没有,你就会变成黑色,Color.Transparent 的 R、G 和 B 为 0。黑色。
No, you made that background red, not black. Use Color.Transparent if you want to have the alpha of the background set to 0. Or just omit the Clear(), it is the default for a new bitmap. And avoid Original.RawFormat in the Save() call, you don't want to use an image format that doesn't support transparency. Png is always good. And be sure that whatever method you use to display the resulting bitmap supports transparency as well. With a well defined background color. You'll get black when it doesn't, Color.Transparent has R, G and B at 0. Black.
你的图像的输入格式是什么?如果是jpg,则可能是因为jpg不支持透明度。您可以尝试使用支持透明度的 PNG 输出格式:
What is the input format of your image? If it is jpg, the it is probably because jpg does not support transparency. You can try to use an output format of PNG which does support transparency: