473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Trying to use Graphics.DrawImage to make transparent image...

Samishii23
246 100+
Expand|Select|Wrap|Line Numbers
  1. public void FadeForm() {
  2.     Bitmap FadeBmp = new Bitmap(Settings[0] + "fade.jpg");
  3.     ColorMatrix CMFade = new ColorMatrix();
  4.     ImageAttributes AFade = new ImageAttributes();
  5.  
  6.     FadeBox = new PictureBox();
  7.     FadeBox.Location = new Point(0,0);
  8.     FadeBox.Size = new Size(this.Width,this.Height);
  9.     FadeBox.Visible = false;
  10.     this.Controls.Add(FadeBox);
  11.  
  12.     CMFade.Matrix33 = 0.4f;
  13.     AFade.SetColorMatrix(CMFade, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  14.     Graphics g = Graphics.FromImage(FadeBmp);
  15.  
  16.     g.DrawImage((Image)FadeBmp,
  17.         new Rectangle(1,1,800,650),
  18.         0,0,
  19.         800,650,
  20.         GraphicsUnit.Pixel,
  21.         AFade);
  22.  
  23.     //g.Dispose();
  24.     FadeBox.Image = FadeBmp;
  25.     FadeBox.Visible = true;
  26.     FadeBox.BringToFront();
  27.     }
Trying to make a transparent image using ColorMatrix, and redrawing the image (fade.jpg), which is just a black image sized at 800,650. When I run the method, nothing happens. No error, or sign that the picture box was even showed.

I was using the tutorial found here.

Anyone know if I'm doing something wrong in the code? Or if I'm missing something. Its my first time doing something like this with System.Drawing.
Mar 16 '10 #1

✓ answered by GaryTexmo

All right, I think I know what the problem is here...

When you draw your image, you're drawing it to the source image. If you draw a transparent copy of your source image on your source image, you're not going to see any change.

What you need to do is to create a new, blank image, then draw to that. They actually have this in the example you linked, but it's tough to spot (caught me too!) so it's not surprising you missed it.

(From that page...)
Expand|Select|Wrap|Line Numbers
  1. Graphics g = Graphics.FromImage(img);
  2.  
  3. g.DrawImage((Image)bmp,
  4.              new Rectangle(1, 1, pictureBox1.Width - 2, pictureBox1.Height - 2),
  5.              0, 0,
  6.              pictureBox1.Width, pictureBox1.Height,
  7.              GraphicsUnit.Pixel,
  8.              attributes);
Notice the bolded? Put this in your code before the graphics line...

Expand|Select|Wrap|Line Numbers
  1. Image img = new Bitmap(FadeBmp.Width, FadeBmp.Height);
... and update your g.DrawImage line and you should be golden :)

11 23698
GaryTexmo
1,501 Expert 1GB
All right, I think I know what the problem is here...

When you draw your image, you're drawing it to the source image. If you draw a transparent copy of your source image on your source image, you're not going to see any change.

What you need to do is to create a new, blank image, then draw to that. They actually have this in the example you linked, but it's tough to spot (caught me too!) so it's not surprising you missed it.

(From that page...)
Expand|Select|Wrap|Line Numbers
  1. Graphics g = Graphics.FromImage(img);
  2.  
  3. g.DrawImage((Image)bmp,
  4.              new Rectangle(1, 1, pictureBox1.Width - 2, pictureBox1.Height - 2),
  5.              0, 0,
  6.              pictureBox1.Width, pictureBox1.Height,
  7.              GraphicsUnit.Pixel,
  8.              attributes);
Notice the bolded? Put this in your code before the graphics line...

Expand|Select|Wrap|Line Numbers
  1. Image img = new Bitmap(FadeBmp.Width, FadeBmp.Height);
... and update your g.DrawImage line and you should be golden :)
Mar 16 '10 #2
Samishii23
246 100+
Edit:
Ok I get the thing to display... Now the problem is, is the transparency isn't working lol.
Heres my updated function...
Expand|Select|Wrap|Line Numbers
  1. public void FadeForm() {
  2.     ColorMatrix CMFade = new ColorMatrix();
  3.     ImageAttributes AFade = new ImageAttributes();
  4.  
  5.     FadeBox = new PictureBox();
  6.     FadeBox.Location = new Point(0,0);
  7.     FadeBox.Size = new Size(this.Width, (this.Height - MenuBar.Height));
  8.     FadeBox.Visible = false;
  9.     this.Controls.Add(FadeBox);
  10.  
  11.     CMFade.Matrix33 = 0.4f;
  12.     AFade.SetColorMatrix(CMFade, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  13.     Bitmap img = new Bitmap(this.Width,this.Height);
  14.     Graphics g = Graphics.FromImage(img);
  15.     g.DrawImage((Image)img,
  16.         new Rectangle(1, 1, 800, (this.Height - MenuBar.Height)),
  17.         0,0,
  18.         800,(this.Height - MenuBar.Height),
  19.         GraphicsUnit.Pixel,
  20.         AFade);
  21.  
  22.     //g.Dispose();
  23.     FadeBox.Image = img;
  24.     FadeBox.Visible = true;
  25.     FadeBox.BringToFront();
  26.     }
I added (this.Height - MenuBar.Height) because I have my menu bar at the bottom of the form, and in the hopes of making the transparent image over everything besides the menu, though when I run it, it still covers the MenuBar, which is a StatusStrip element btw...

Also the whole form just blacks out. I removed the fade.jpg. Does transparency even work in general?
Mar 16 '10 #3
GaryTexmo
1,501 Expert 1GB
In this example, bmp is the source image. In your code, this would be FadeBmp. You need to pass the destination (the one you used to create the graphics object) to the PictureBox.

Read the doc for the Graphics.DrawImage method to see what that first parameter actually means, that should help clarify things for you :)
Mar 16 '10 #4
Samishii23
246 100+
Updated above post before noticing you had replied to the old one. Sorry.
Just going to use BringToFront() for the Menu until I can work around the image height thing. =\
Mar 16 '10 #5
GaryTexmo
1,501 Expert 1GB
You've eliminated your source image now. Like I said, check the doc... you need two Image objects. One is a destination (what you copy to) and the other is a source (one you copy from). Your code only has one image... big hint: lines 14 and 15).

As for the menu part, again, read the doc on what those parameters actually mean. Again, one is a source location and the other is a destination location.

Another option may be to just change the location/size of your picturebox so it doesn't overlap with your menu.

(http://msdn.microsoft.com/en-us/libr...drawimage.aspx)
Mar 16 '10 #6
Samishii23
246 100+
Unfortunally, after reading the MSDN articles. I still don't see what I'm missing.

I've put the Source image back in. But no transparency...
Expand|Select|Wrap|Line Numbers
  1. public void FadeForm() {
  2.     ColorMatrix CMFade = new ColorMatrix();
  3.     ImageAttributes AFade = new ImageAttributes();
  4.     Bitmap FadeBmp = new Bitmap(Settings[0] + "fade.jpg");
  5.  
  6.     FadeBox = new PictureBox();
  7.     FadeBox.Location = new Point(0,0);
  8.     FadeBox.Size = new Size(this.Width, (this.Height - MenuBar.Height));
  9.     FadeBox.Visible = false;
  10.     this.Controls.Add(FadeBox);
  11.  
  12.     CMFade.Matrix33 = 0.5f;
  13.     AFade.SetColorMatrix(CMFade, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  14.     Bitmap img = new Bitmap(FadeBox.Width, FadeBox.Height);
  15.     Graphics g = Graphics.FromImage(FadeBmp);
  16.     g.DrawImage((Image)img,
  17.         new Rectangle(1, 1, img.Width, img.Height),
  18.         0,0,
  19.         FadeBox.Width,FadeBox.Width,
  20.         GraphicsUnit.Pixel,
  21.         AFade);
  22.     //g.Dispose();
  23.  
  24.     FadeBox.Image = img;
  25.     FadeBox.Visible = true;
  26.     FadeBox.BringToFront();
  27.     MenuBar.BringToFront();
  28.     }
I just don't see the problem. *sigh*
Mar 16 '10 #7
GaryTexmo
1,501 Expert 1GB
Lines 15 and 16 look like this..

Expand|Select|Wrap|Line Numbers
  1.      Graphics g = Graphics.FromImage(FadeBmp);
  2.      g.DrawImage((Image)img,
They need to look like this...

Expand|Select|Wrap|Line Numbers
  1.      Graphics g = Graphics.FromImage(img);
  2.      g.DrawImage((Image)FadeBmp,
img is where you're drawing to, so you create the graphics on that image. FadeBmp is what you want to draw, so you use that in your draw image method. Graphics.DrawImage will draw the supplied image to the target graphics (which was created as the handle to img).

Thus, the semi-opaque copy of FadeBmp is drawn onto the blank img object.

Note, Bitmap doesn't support transparency by normally... I think it's an indexing thing, so you might need to change the object type to Image, but still create a new Bitmap. I know, it's weird, but if you're not getting good results, that's the first thing to try.

I had this working in a test project that I deleted, but I'm pretty sure you're really close. Give that a try and let me know.

*Edit: I just tested using Bitmap and it's fine, should still work for ya.
Mar 16 '10 #8
Samishii23
246 100+
Ok. I changed the two lines. Same thing. Still looks like a black canvas. There is no transparency. I even tried making them Images, not Bitmap...
I kinda feel like a jerk saying this over and over again... I don't know why your side always works and mine doesn't. =\
Mar 16 '10 #9
GaryTexmo
1,501 Expert 1GB
Hmm, that's very confusing. Are you sure you're not assigning the black image to the panel anywhere somehow? Here's the code for what I have... it's basically exactly the same as yours, but maybe there's a difference I'm not seeing.

Expand|Select|Wrap|Line Numbers
  1.         public Form1()
  2.         {
  3.             InitializeComponent();
  4.  
  5.             Bitmap sourceImg = new Bitmap(@"c:\temp\test.bmp");
  6.  
  7.             ColorMatrix matrix = new ColorMatrix();
  8.             matrix.Matrix33 = 0.1f;
  9.  
  10.             ImageAttributes attributes = new ImageAttributes();
  11.             attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  12.  
  13.             Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
  14.             Graphics g = Graphics.FromImage(destImg);
  15.             g.DrawImage(
  16.                 (Image)sourceImg,
  17.                 new Rectangle(0, 0, destImg.Width, destImg.Height),
  18.                 0, 0,
  19.                 sourceImg.Width, sourceImg.Height,
  20.                 GraphicsUnit.Pixel,
  21.                 attributes);
  22.  
  23.             pictureBox1.Image = destImg;
  24.         }
Created a new project and put a picture box on it, then had this code for my constructor. When I run, I have a light gray box... my source picture is just a 100x100 bitmap that's all black.
Mar 16 '10 #10
Samishii23
246 100+
I copied and pasted my Function into a test project... Woo, it worked.......
Great. Now I don't know why it won't work on my project... ...

Now if I can only figure out what the difference is...
Mar 17 '10 #11
Samishii23
246 100+
Ugh. Seriously. Found out the problem...

The PictureBox control was inheriting the Windows background color. Which was set to Black... Seriously. Only had to set the BackColor property to Transparent.

This feels like something I should have known to do from the start... Yet it evaded my notice... Bah!

Thank you for the help yet again Gary!
Mar 17 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: news.microsoft.com | last post by:
Hello group, My goal is to attach an image over another image. Top image should be transparent so the back image is visible through the top one. Bellow is a test code in VB.NET. You need to...
3
by: Jeroen Ceuppens | last post by:
Hi, When i do this in my programma, in the contstructor of the form: g.DrawImage(i,new Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel); it doens't paint it ;( but if...
5
by: Frecklefoot | last post by:
I'm using DrawImage() to copy one image onto another, larger one. I'm using a DrawImage() overload to stretch the source image onto the destination. The problem is that when it draws the stretched...
4
by: Keith Smith | last post by:
I am making a program that needs to put a copy of a certain small image wherever the user clicks with the mouse. I am using a pictureBox as my main area (although I can change this if necessary). ...
1
by: Robert W. | last post by:
In my Winforms app I'm trying to get an image's background to appear transparent on a form that has a gradient background. So I added a PictureBox and then attempted to add a custom paint command...
13
by: SStory | last post by:
I need to be able to give the appearance of glass over a picture inside of a frame. How can I do this in real time given that the picture will change? Is there a way using the graphics to make...
2
by: Kalvin | last post by:
I keep getting my string inside a black box with this code. I would like to make the background transparent. Any help on this will be appreciated very much. '**** BEGIN CODE **** Try Dim...
2
by: kirkage | last post by:
I am attempting to create a program in C# which part of it consists of one image fading to another. The best way I found this to work is from an example by Bob Powell on the thread...
13
by: Joe | last post by:
does anyone know why this source code throws an error?? Dim myBitmap As System.Drawing.Bitmap Dim myGraphics As Graphics myBitmap = New System.Drawing.Bitmap(fileName:="C:\test.tif")...
9
by: DaveL | last post by:
hello I have a Bit map 1367 wide 32 high this bitmap contains like 40 separate Images 32x32 I tell it the id*32 to get the approiate Image from the source Bitmap When i CreateGraphics()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.