Click here to Skip to main content
15,943,798 members
Articles / Multimedia / GDI+

Image Reflection

Rate me:
Please Sign up or sign in to vote.
4.76/5 (24 votes)
8 Jun 2009CPOL1 min read 59.1K   2.4K   40   17
Learn how to generate a reflection from your selected image


This source code will teach you how to create a simple GDI+ Glass Reflection effect in C#.NET.   

The Codes  

Make sure to include these namespaces:


Here's the DrawImage code. It gets the image from the pictureBox1, then creates a reflection.

private static Image DrawReflection(Image img,Color toBG) // img is the original image.
    //This is the static function that generates the reflection...
    int height = img.Height + 100; //Added height from the original height of the image.
    Bitmap bmp = new Bitmap(img.Width, height, PixelFormat.Format64bppPArgb); //A new 
    //The Brush that generates the fading effect to a specific color of your background.
    Brush brsh = new LinearGradientBrush(new Rectangle(0, 0, img.Width + 10, 
      height), Color.Transparent, toBG, LinearGradientMode.Vertical);
    bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution); //Sets the new 
							//bitmap's resolution.
    using(Graphics grfx = Graphics.FromImage(bmp)) //A graphics to be generated 
			//from an image (here, the new Bitmap we've created (BMP)).
        Bitmap bm = (Bitmap)img; //Generates a bitmap from the original image (img).
        grfx.DrawImage(bm, 0, 0, img.Width, img.Height); //Draws the generated 
					//bitmap (bm) to the new bitmap (bmp).
        Bitmap bm1 = (Bitmap)img; 	//Generates a bitmap again 
				//from the original image (img).
        bm1.RotateFlip(RotateFlipType.Rotate180FlipX); //Flips and rotates the 
						//image (bm1).
        grfx.DrawImage(bm1, 0, img.Height); 	//Draws (bm1) below (bm) so it serves 
					//as the reflection image.
        Rectangle rt = new Rectangle(0, img.Height, img.Width, 100); //A new rectangle 
						//to paint our gradient effect.
        grfx.FillRectangle(brsh, rt); //Brushes the gradient on (rt).
    return bmp; //Returns the (bmp) with the generated image.

Now after creating this function, use it to change the image of the picturebox through a click event or other events.

private void controlname_click(Object sender, EventArgs e)
pictureBox1.Image = DrawReflection(pictureBox1.Image, Color.Black);

This code changes the current pictureBox1's image to the image generated by the DrawReflection.  

How Does It Work?

This is the result if the image is not painted with gradient.


Explanation for the gradient:


The flipped image below the original image has a fading effect because of the gradient we painted on it. The starting color is Transparent. We can see the image below it. And as it goes down, it changes to color black, which is the color of the form, so the lower portion of the image blends with the background color, creating a fading effect.

What Problems Does This Solution Solve?

I made this solution so that beginner coders could better understand the Image Reflection in C#.NET. It is made in the Main Form's class. 

How Does This Help Someone?

This helps better understanding of the usage of the System.Drawing classes and functions.


  • 6/03/09 - Initial release (this source code)


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

Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionSupport for grandient background? Pin
Member 1119976012-Dec-14 12:05
Member 1119976012-Dec-14 12:05 
GeneralVotes Pin
VovaZ3-Apr-12 4:39
VovaZ3-Apr-12 4:39 
GeneralMy vote of 5 Pin
walterhevedeich26-May-11 18:48
professionalwalterhevedeich26-May-11 18:48 
QuestionHow about fade to transparent? Pin
JBenhart2-Feb-10 18:14
JBenhart2-Feb-10 18:14 
AnswerRe: How about fade to transparent? Pin
Espiritu John2-Feb-10 21:40
professionalEspiritu John2-Feb-10 21:40 
GeneralRe: How about fade to transparent? Pin
JBenhart3-Feb-10 9:30
JBenhart3-Feb-10 9:30 
QuestionCan it be made to work for any background colour ... Pin
Jeremy Bradshaw25-Nov-09 8:48
Jeremy Bradshaw25-Nov-09 8:48 
AnswerRe: Can it be made to work for any background colour ... Pin
Espiritu John4-Dec-09 21:55
professionalEspiritu John4-Dec-09 21:55 
GeneralExcellent Article Pin
Jason Down18-Jun-09 4:00
Jason Down18-Jun-09 4:00 
GeneralRe: Excellent Article Pin
Espiritu John18-Jun-09 23:21
professionalEspiritu John18-Jun-09 23:21 
GeneralSaving! Pin
MARC5616-Jun-09 4:46
MARC5616-Jun-09 4:46 
GeneralRe: Saving! Pin
Espiritu John18-Jun-09 23:30
professionalEspiritu John18-Jun-09 23:30 
GeneralThat's how an article for begginers should be... Pin
Daniel Turini16-Jun-09 0:01
Daniel Turini16-Jun-09 0:01 
GeneralRe: That's how an article for begginers should be... Pin
Espiritu John18-Jun-09 23:31
professionalEspiritu John18-Jun-09 23:31 
GeneralNice! Pin
NiteBeast8-Jun-09 16:38
NiteBeast8-Jun-09 16:38 
GeneralRe: Nice! Pin
jfcosse8-Jun-09 20:02
jfcosse8-Jun-09 20:02 
GeneralRe: Nice! Pin
Espiritu John3-Nov-09 0:33
professionalEspiritu John3-Nov-09 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.