Sometimes, we are needed to make an image transparent-.to use it as an watermark or something simillar. We need to use code as following to convert an existing image to an transparent one.
public Bitmap createwatermark(string filepath)
{
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 0.3f, 0}, // the fourth value (0.3f) is the alpha value for partial transparency....
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
Bitmap sourceimage = new Bitmap(filepath);
Bitmap bitmap = new Bitmap(sourceimage.Width, sourceimage.Height);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(sourceimage, new Rectangle(0, 0, bitmap.Width, bitmap.Height),0.0f,0.0f,sourceimage.Height,sourceimage.Height,GraphicsUnit.Pixel,imageAtt);
return bitmap;
}
public Bitmap createwatermark(string filepath)
{
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 0.3f, 0}, // the fourth value (0.3f) is the alpha value for partial transparency....
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
Bitmap sourceimage = new Bitmap(filepath);
Bitmap bitmap = new Bitmap(sourceimage.Width, sourceimage.Height);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(sourceimage, new Rectangle(0, 0, bitmap.Width, bitmap.Height),0.0f,0.0f,sourceimage.Height,sourceimage.Height,GraphicsUnit.Pixel,imageAtt);
return bitmap;
}
No comments:
Post a Comment