-
public void FadeForm() {
-
Bitmap FadeBmp = new Bitmap(Settings[0] + "fade.jpg");
-
ColorMatrix CMFade = new ColorMatrix();
-
ImageAttributes AFade = new ImageAttributes();
-
-
FadeBox = new PictureBox();
-
FadeBox.Location = new Point(0,0);
-
FadeBox.Size = new Size(this.Width,this.Height);
-
FadeBox.Visible = false;
-
this.Controls.Add(FadeBox);
-
-
CMFade.Matrix33 = 0.4f;
-
AFade.SetColorMatrix(CMFade, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
-
Graphics g = Graphics.FromImage(FadeBmp);
-
-
g.DrawImage((Image)FadeBmp,
-
new Rectangle(1,1,800,650),
-
0,0,
-
800,650,
-
GraphicsUnit.Pixel,
-
AFade);
-
-
//g.Dispose();
-
FadeBox.Image = FadeBmp;
-
FadeBox.Visible = true;
-
FadeBox.BringToFront();
-
}
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.
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...) - Graphics g = Graphics.FromImage(img);
-
-
g.DrawImage((Image)bmp,
-
new Rectangle(1, 1, pictureBox1.Width - 2, pictureBox1.Height - 2),
-
0, 0,
-
pictureBox1.Width, pictureBox1.Height,
-
GraphicsUnit.Pixel,
-
attributes);
Notice the bolded? Put this in your code before the graphics line... - Image img = new Bitmap(FadeBmp.Width, FadeBmp.Height);
... and update your g.DrawImage line and you should be golden :)
11 23698
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...) - Graphics g = Graphics.FromImage(img);
-
-
g.DrawImage((Image)bmp,
-
new Rectangle(1, 1, pictureBox1.Width - 2, pictureBox1.Height - 2),
-
0, 0,
-
pictureBox1.Width, pictureBox1.Height,
-
GraphicsUnit.Pixel,
-
attributes);
Notice the bolded? Put this in your code before the graphics line... - Image img = new Bitmap(FadeBmp.Width, FadeBmp.Height);
... and update your g.DrawImage line and you should be golden :)
Edit:
Ok I get the thing to display... Now the problem is, is the transparency isn't working lol.
Heres my updated function... - public void FadeForm() {
-
ColorMatrix CMFade = new ColorMatrix();
-
ImageAttributes AFade = new ImageAttributes();
-
-
FadeBox = new PictureBox();
-
FadeBox.Location = new Point(0,0);
-
FadeBox.Size = new Size(this.Width, (this.Height - MenuBar.Height));
-
FadeBox.Visible = false;
-
this.Controls.Add(FadeBox);
-
-
CMFade.Matrix33 = 0.4f;
-
AFade.SetColorMatrix(CMFade, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
-
Bitmap img = new Bitmap(this.Width,this.Height);
-
Graphics g = Graphics.FromImage(img);
-
g.DrawImage((Image)img,
-
new Rectangle(1, 1, 800, (this.Height - MenuBar.Height)),
-
0,0,
-
800,(this.Height - MenuBar.Height),
-
GraphicsUnit.Pixel,
-
AFade);
-
-
//g.Dispose();
-
FadeBox.Image = img;
-
FadeBox.Visible = true;
-
FadeBox.BringToFront();
-
}
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?
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 :)
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. =\
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)
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... - public void FadeForm() {
-
ColorMatrix CMFade = new ColorMatrix();
-
ImageAttributes AFade = new ImageAttributes();
-
Bitmap FadeBmp = new Bitmap(Settings[0] + "fade.jpg");
-
-
FadeBox = new PictureBox();
-
FadeBox.Location = new Point(0,0);
-
FadeBox.Size = new Size(this.Width, (this.Height - MenuBar.Height));
-
FadeBox.Visible = false;
-
this.Controls.Add(FadeBox);
-
-
CMFade.Matrix33 = 0.5f;
-
AFade.SetColorMatrix(CMFade, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
-
Bitmap img = new Bitmap(FadeBox.Width, FadeBox.Height);
-
Graphics g = Graphics.FromImage(FadeBmp);
-
g.DrawImage((Image)img,
-
new Rectangle(1, 1, img.Width, img.Height),
-
0,0,
-
FadeBox.Width,FadeBox.Width,
-
GraphicsUnit.Pixel,
-
AFade);
-
//g.Dispose();
-
-
FadeBox.Image = img;
-
FadeBox.Visible = true;
-
FadeBox.BringToFront();
-
MenuBar.BringToFront();
-
}
I just don't see the problem. *sigh*
Lines 15 and 16 look like this.. - Graphics g = Graphics.FromImage(FadeBmp);
-
g.DrawImage((Image)img,
They need to look like this... - Graphics g = Graphics.FromImage(img);
-
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.
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. =\
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. - public Form1()
-
{
-
InitializeComponent();
-
-
Bitmap sourceImg = new Bitmap(@"c:\temp\test.bmp");
-
-
ColorMatrix matrix = new ColorMatrix();
-
matrix.Matrix33 = 0.1f;
-
-
ImageAttributes attributes = new ImageAttributes();
-
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
-
-
Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
-
Graphics g = Graphics.FromImage(destImg);
-
g.DrawImage(
-
(Image)sourceImg,
-
new Rectangle(0, 0, destImg.Width, destImg.Height),
-
0, 0,
-
sourceImg.Width, sourceImg.Height,
-
GraphicsUnit.Pixel,
-
attributes);
-
-
pictureBox1.Image = destImg;
-
}
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.
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...
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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). ...
|
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...
|
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...
|
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...
|
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...
|
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")...
|
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()...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |