An other image processing
-
hi again friends, how can i change my image opacity?
-
hi again friends, how can i change my image opacity?
One way would be to change the pixel format of the image to ARGB and then save it as PNG or TIFF file, like that:
public static Image SetImgOpacity(Image imgPic, float imgOpac)
{
Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
Graphics gfxPic = Graphics.FromImage(bmpPic);
ColorMatrix cmxPic = new ColorMatrix();cmxPic.Matrix33 = imgOpac;
ImageAttributes iaPic = new ImageAttributes();
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
gfxPic.Dispose();return bmpPic;
}using (Image img = Image.FromFile(filename))
{
Image img2 = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format32bppArgb);
img2 = SetImgOpacity(img2, 0.5f);
img2.Save(filename2, ImageFormat.Png);
img2.Dispose();
}I got the SetImgOpacity method from here[^]. I just tested this code and it works fine. regards
-
One way would be to change the pixel format of the image to ARGB and then save it as PNG or TIFF file, like that:
public static Image SetImgOpacity(Image imgPic, float imgOpac)
{
Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
Graphics gfxPic = Graphics.FromImage(bmpPic);
ColorMatrix cmxPic = new ColorMatrix();cmxPic.Matrix33 = imgOpac;
ImageAttributes iaPic = new ImageAttributes();
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
gfxPic.Dispose();return bmpPic;
}using (Image img = Image.FromFile(filename))
{
Image img2 = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format32bppArgb);
img2 = SetImgOpacity(img2, 0.5f);
img2.Save(filename2, ImageFormat.Png);
img2.Dispose();
}I got the SetImgOpacity method from here[^]. I just tested this code and it works fine. regards
you're really great, merci
-
you're really great, merci
What I just tried out which works, you can also convert the image to grayscale with the ColorMatrix class used above, just set the matrix to
cmxPic.Matrix00 = 0.299f;
cmxPic.Matrix01 = 0.299f;
cmxPic.Matrix02 = 0.299f;cmxPic.Matrix10 = 0.587f;
cmxPic.Matrix11 = 0.587f;
cmxPic.Matrix12 = 0.587f;cmxPic.Matrix20 = 0.114f;
cmxPic.Matrix21 = 0.114f;
cmxPic.Matrix22 = 0.114f;This will produce a grayscale image with a different approach as presented in the aricles. Hope this helps :)