开发者

how to draw an image on a canvas with transparency/alpha

开发者 https://www.devze.com 2023-01-07 17:20 出处:网络
hy there! i left out usings etc... just plain code: var image = Image.FromFile(/* my magic source */); var bitmap = new Bitmap(image.Width, image.Height);

hy there!

i left out usings etc... just plain code:

var image = Image.FromFile(/* my magic source */);
var bitmap = new Bitmap(image.Width, image.Height);
var canvas = Graphics.FromImage(bitmap);
var brush = new SolidBrush(/* my magic color */);
canvas.FillRectangle(brush, 0, 0, image.Width, image.Height);
canvas.DrawImage(image, new Rec开发者_如何学Gotangle(0, 0, image.Width, image.Height));
canvas.Save();
bitmap.Save(/* my magic target */);

i want to draw image with alpha 55% on canvas. image is a .png-file and uses transparency itself. (NOTE: i do not want to make image.MakeTransparent() - it is already transparent, i just need some alpha-effect)

how can i achieve this?


Try ColorMatrix and ImageAttributes:

ColorMatrix cm = new ColorMatrix();
cm.Matrix33 = 0.55f;
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm);
canvas.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ia);
0

精彩评论

暂无评论...
验证码 换一张
取 消