Click here to Skip to main content
15,894,223 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to make an editor for trasparent images, but how can i draw a trasparent image?
I'd like also to draw that effect like some editor (like gimp and photo shop) where there is the trasparency...
Posted

First of all, what does it mean, "transparent image"? If only makes sense if you have some graphics "below the image" (in Z order), and, if your image is semi-transparent, you can see the background graphics through. If you put your image, say, on uniform white or black background, you cannot say if it is semi-transparent or not.

But, with PictureBox, it may make no sense. It has only one image. To ask about transparency, you need to explain how it should be manifested. There is no transparency per se.

If this is understood, let's see how you can have the effect of transparency. If would suggest to forget about PictureBox, as something distracting from the essence of the problem and totally useless in practice.

To understand why you should forget about PictureBox, please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

These answers also explain what could you do instead. You need to render the image on some control using System.Drawing.Graphics. You can, for example, draw some graphics, and, on top of it, draw some image:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage.aspx[^].

In general case, some images support transparency. For example, you can draw a PNG image and load if from a file or some other stream:
http://en.wikipedia.org/wiki/Portable_Network_Graphics#Transparency_of_image[^].

If you draw such kind of image over some graphics, you can see parts or all of this graphics through your image, depending on its transparency map.

Moreover, you can draw any other graphical elements (lines, rectangles and so on) with transparent properties if you simply use the colors with alpha-channel representing transparency. Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.color.fromargb.aspx[^].

In the term "ARGB", "A" mean alpha-channel, opacity:
http://msdn.microsoft.com/en-us/library/system.drawing.color.a.aspx[^].

—SA
 
Share this answer
 
Comments
[no name] 27-Apr-14 10:55am    
+5.

It is a bad style downgrade an answer, just because this is described in detail and the reader demanded to familiarize themselves.
Sergey Alexandrovich Kryukov 27-Apr-14 12:19pm    
Thank you, Bruno.

We don't know why such down-voted are posted; I can a set of down-votes following in short period of time, which might be the indication that some vote not against the answer but against a person... Sometimes it happens; I don't care much... :-)

—SA
[no name] 27-Apr-14 13:58pm    
It is self-explanatory when it comes to a person then it is superfluous anyway in this forum. I know I was wrong in the same way in past....but I learned...not least by you (keep in mind Google helped me to translate). Regards, Bruno
I solved:

C#
var bmp = new Bitmap( sourceImage.Width, sourceImage.Height );
            var g = Graphics.FromImage( bmp );

            for( int j = 0; j < bmp.Height / 9; j++ )
            for( int i = j % 2; i < bmp.Width / 9; i+=2 ) g.FillRectangle(new SolidBrush(Color.Gray), 9 * i, 9*j, 9, 9);

            g.DrawImage( sourceImage, new Point(0,0) );
            g.Dispose();

            var attr = new ImageAttributes();

            var dstRect = new Rectangle( 0, 0, bmp.Width, bmp.Height );
            e.Graphics.DrawImage( bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
                GraphicsUnit.Pixel, attr );
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900