C# 我保存的图像显示为黑色-更改图像的不透明度
我试图在更改图像不透明度后保存图像这是我的代码:C# 我保存的图像显示为黑色-更改图像的不透明度,c#,.net,gdi+,C#,.net,Gdi+,我试图在更改图像不透明度后保存图像这是我的代码: protected void bntChangeOpacity_Click(object sender, EventArgs e) { String saveDir = mydir; Image watermarkImage = Image.FromFile(Server.MapPath(mydir + "imgname.jpg")); Graphics gr = Graphics.FromIm
protected void bntChangeOpacity_Click(object sender, EventArgs e) {
String saveDir = mydir;
Image watermarkImage = Image.FromFile(Server.MapPath(mydir + "imgname.jpg"));
Graphics gr = Graphics.FromImage(watermarkImage);
Rectangle r2 = new Rectangle(new Point(0, 0), new Size(watermarkImage.Width, watermarkImage.Height));
float opacityvalue = 0.5f;
ImageUtils.ImageTransparency.ChangeOpacity(watermarkImage, opacityvalue);
Bitmap b1 = new Bitmap(watermarkImage.Width, watermarkImage.Height);
gr.DrawImage(watermarkImage, r2);
b1.Save(Server.MapPath(saveDir + "sasf.jpg"));
}
课程代码为:
public class ImageTransparency {
public static Bitmap ChangeOpacity(Image img, float opacityvalue)
{
Bitmap bmp = new Bitmap(img.Width,img.Height);
Graphics graphics = Graphics.FromImage(bmp);
ColorMatrix colormatrix = new ColorMatrix();
colormatrix.Matrix33 = opacityvalue;
ImageAttributes imgAttribute = new ImageAttributes();
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
graphics.Dispose();
return bmp;
}
}
有什么问题吗??请帮助您可以这样做:
private void button1_Click(object sender, EventArgs e)
{
float opacityvalue = 0.5f;
var img= ImageTransparency.ChangeOpacity(Image.FromFile(@"PathToYourImage.png"), opacityvalue);
img.Save(@"PathToYourImage-Opacity.png");
}
class ImageTransparency
{
public static Bitmap ChangeOpacity(Image img, float opacityvalue)
{
Bitmap bmp = new Bitmap(img.Width,img.Height); // Determining Width and Height of Source Image
Graphics graphics = Graphics.FromImage(bmp);
ColorMatrix colormatrix = new ColorMatrix();
colormatrix.Matrix33 = opacityvalue;
ImageAttributes imgAttribute = new ImageAttributes();
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
graphics.Dispose(); // Releasing all resource used by graphics
return bmp;
}
}
基于i,我使用浮点数opacityvalue=0.5f;所以colormatrix.Matrix33将采用该值。。错了吗@CommuSofti收到以下错误:System.Runtime.InteropServices.ExternalException:GDI+中发生一般错误。打开:img.Save(@“mydir\img.jpg”);完成了,非常感谢我更改了img.Save(@“mydir\img.jpg”);保存(Server.MapPath(“mydir/imgnew.jpg”)@H.A.M很高兴听到你成功了。不客气:)