2010-07-19 11 views
6

hy there!wie zeichne ein bild auf einer leinwand mit transparenz/alpha

links i usings usw. aus ... einfach nur 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 Rectangle(0, 0, image.Width, image.Height)); 
canvas.Save(); 
bitmap.Save(/* my magic target */); 

ich will auf canvasimage mit alpha 55% zeichnen. image ist eine .png-Datei und verwendet Transparenz selbst. (HINWEIS: Ich möchte nicht machen image.MakeTransparent() - es ist bereits transparent, ich brauche nur einige Alpha-Effekt)

Wie kann ich das erreichen?

Antwort

14

Versuchen Colormatrix und 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

wie Charme zu arbeiten! Vielen Dank! –

+7

Ihre Überladung von 'DrawImage' existiert nicht: Ich habe verwendet:' canvas.DrawImage (Bild, neues Rectangle (0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height , GraphicsUnit.Pixel, imageAttributes); ' –

+1

Dieser Weg scheint nur für Rgb24 Pixel Format zu funktionieren. Ich habe Argb32 oder Indexed8bpp versucht, es funktioniert nicht. – IlPADlI

Verwandte Themen