[英]My saved image appears black - Changing opacity of image
im trying to save an image after changing its opacity here is my code: 我试图更改图像的不透明度后保存图像,这是我的代码:
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"));
}
the class code is: 类代码为:
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;
}
}
whats the problem ?? 有什么问题 ?? please help
请帮忙
You can do it this way: 您可以这样操作:
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;
}
}
Based on Change Opacity of Image in C# 基于C#中图像的更改不透明度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.