SIGN UP MEMBER LOGIN:    
ARTICLE

Viewing Multiple Images

Posted by Mahesh Chand Articles | GDI+ & Graphics May 08, 2002
I'm writing this article in response to a question on discussion forums, How do I view multiple images on top of each other?
Reader Level:
Download Files:
 

I'm writing this article in response to a question on discussion forums, How do I view multiple images on top of each other? Basically the guy has two images. One is a large image and second image is a small image and he wants to view small image on top of the large one. Even though I answered it on the forums and I knew that this is the answer, but I thought to test it by myself.

I also thought, It would be nice if I've some kind of counter so I can set the transparency of the small image to see through it. In case some body ask this question again ;).

Any way, this is what application looks like. As you can see from Figure 1, I've one track bar control, which sets the transparency of small image. 



Figure 1.

I add a trackBar control to the form. Make sure its Maximum and minimum properties are 10 and 0 respectively. See Figure 2.

Now I write the trackBar control scroll event so when you scroll the track bar, it should manage the transparency of the image. I forgot to tell you that I defined a float type variable in the biggining of the project.

float tpVal = 1.0f;

Now I convert the value of trackbar control to a floating value so I can use in the ColorMatrix to set the color of the image. See Drawing Transparent Images and Shapes Using Alpha Blending for more on how to draw transparent graphics objects in GDI+.

private void trackBar1_Scroll(object sender, System.EventArgs e)
{
tpVal = (
float)trackBar1.Value/10;
this.Invalidate();
}

In the end, I view both images on the form's paint event as you can see from the following code.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Image curImage = Image.FromFile("roses.jpg");
e.Graphics.DrawImage(curImage, AutoScrollPosition.X, AutoScrollPosition.Y,
curImage.Width, curImage.Height );
float[][] ptsArray ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, tpVal, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix clrMatrix =
new ColorMatrix(ptsArray);
ImageAttributes imgAttributes =
new ImageAttributes();
imgAttributes.SetColorMatrix(clrMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
Image smallImage = Image.FromFile("smallRoses.gif");
e.Graphics.DrawImage(smallImage,
new Rectangle(100, 100, 100, 100),
0, 0, smallImage.Width, smallImage.Height,
GraphicsUnit.Pixel, imgAttributes );
}
 

Wonder where ColorMatrix, ImageAttributes and other code came from? See Drawing Transparent Images and Shapes Using Alpha Blending for the answer.

Login to add your contents and source code to this article
share this article :
post comment
 

100/100

Posted by saifullah khan Nov 03, 2010

How can we display or load all the images from a folder on to Windows Form? i am working to have a tree view of folders in left window and display of al the images as thumbnails in right window. Thanks in advance, any help is appriciated. Pallavi

Posted by Pallavi Feb 04, 2008
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor