Click here to Skip to main content
6,629,885 members and growing! (23,862 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Image Display License: The Code Project Open License (CPOL)

Image Reflection

By VistaHacker

Learn how to generate a reflection from your selected image
C# (C# 1.0, C# 2.0, C# 3.0), Windows (WinXP, Vista), .NET (.NET 3.5), GDI+
Version:12 (See All)
Posted:8 Jun 2009
Views:4,525
Bookmarked:26 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 5.18 Rating: 4.65 out of 5

1

2
1 vote, 7.7%
3
4 votes, 30.8%
4
8 votes, 61.5%
5
GDI__Glass_Reflection

Introduction

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:

System.Drawing; 
System.Drawing.Drawing2D; 
System.Drawing.Imaging;

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 
								//bitmap.
    //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.

Image_without_gradient.png

Explanation for the gradient:

Image.png

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.

History

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

License

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

About the Author

VistaHacker


Member
I'm an independent software developer. I had learned programming at the age of 14. I started to program in Visual Basic 6. After having a new and faster computer, I've moved on to Visual Basic.net 2008, then to C#.net 2008.
Occupation: Software Developer
Company: [Independent Software Developer]
Location: Philippines Philippines

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralExcellent Article PinmemberJason Down5:00 18 Jun '09  
GeneralRe: Excellent Article PinmemberVistaHacker0:21 19 Jun '09  
GeneralSaving! PinmemberMARC565:46 16 Jun '09  
GeneralRe: Saving! PinmemberVistaHacker0:30 19 Jun '09  
GeneralThat's how an article for begginers should be... PinmemberDaniel Turini1:01 16 Jun '09  
GeneralRe: That's how an article for begginers should be... PinmemberVistaHacker0:31 19 Jun '09  
GeneralNice! PinmemberNiteBeast17:38 8 Jun '09  
GeneralRe: Nice! Pinmemberjfcosse21:02 8 Jun '09  
GeneralRe: Nice! PinmemberVistaHacker1:33 3 Nov '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by VistaHacker
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project