删除图像调整大小透明度后 - gdi+
After image resize transparency is removed - gdi+
我正在从包含中心透明区域(帧图像类型)的文件中读取图像。
Image myFrame = Image.FromFile("d:\mypngfile.png");
调用图像调整大小自定义函数后:
myFrame = resizeImage(myFrame, new Size(otherbmp.Width, otherbmp.Height));
问题是在调整图像后透明度似乎被移除了。
调整大小函数:
public Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight,PixelFormat.Format32bppArgb);
b.MakeTransparent();
Graphics g = Graphics.FromImage((Image)b);
g.CompositingQuality = CompositingQuality.HighQuality;
g.CompositingMode = CompositingMode.SourceOver;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
在我检查调整大小后不起作用的 alpha 像素之后(它在调整大小之前起作用并且return是一个值)。它总是 return 0!
public int GetBorderWidth(Bitmap bmp)
{
var hy = bmp.Height/ 2;
while (bmp.GetPixel(0, hy).A == 255 && sz.Width < hx)
sz.Width++;
return sz.width;
}
无论我怎样尝试,我似乎都无法解决这个问题...
我发现了这个问题,它与调整图像大小后边框似乎有一些透明像素有关..(如 Alpha 150)所以边框不是 ALPHA 255..
我做了什么我用
更改了函数 GetBorderWidth 代码
while (bmp.GetPixel(sz.Width, hy).A > 10 && sz.Width < hx)
sz.Width++;
似乎解决了这个问题。
谢谢大家让我知道要检查什么。
我正在从包含中心透明区域(帧图像类型)的文件中读取图像。
Image myFrame = Image.FromFile("d:\mypngfile.png");
调用图像调整大小自定义函数后:
myFrame = resizeImage(myFrame, new Size(otherbmp.Width, otherbmp.Height));
问题是在调整图像后透明度似乎被移除了。
调整大小函数:
public Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight,PixelFormat.Format32bppArgb);
b.MakeTransparent();
Graphics g = Graphics.FromImage((Image)b);
g.CompositingQuality = CompositingQuality.HighQuality;
g.CompositingMode = CompositingMode.SourceOver;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
在我检查调整大小后不起作用的 alpha 像素之后(它在调整大小之前起作用并且return是一个值)。它总是 return 0!
public int GetBorderWidth(Bitmap bmp)
{
var hy = bmp.Height/ 2;
while (bmp.GetPixel(0, hy).A == 255 && sz.Width < hx)
sz.Width++;
return sz.width;
}
无论我怎样尝试,我似乎都无法解决这个问题...
我发现了这个问题,它与调整图像大小后边框似乎有一些透明像素有关..(如 Alpha 150)所以边框不是 ALPHA 255.. 我做了什么我用
更改了函数 GetBorderWidth 代码while (bmp.GetPixel(sz.Width, hy).A > 10 && sz.Width < hx) sz.Width++;
似乎解决了这个问题。 谢谢大家让我知道要检查什么。