[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Resize images and save the results in C#

resize images

This program lets you load and resize images, and save the results into files.

When the form loads, the following Load event handler saves the scale factor for each of the scaling menu items in their Tag properties.

// Set the menus' Tag values. private void Form1_Load(object sender, EventArgs e) { picZoom.SizeMode = PictureBoxSizeMode.StretchImage; mnuScale1_2.Tag = 0.5f; mnuScale1.Tag = 1f; mnuScale2.Tag = 2f; mnuScale4.Tag = 4f; mnuScale6.Tag = 6f; }

For example, this code sets the x1/2 menu item's Tag property to 0.5f. Similarly it sets the x4 menu item's Tag property to 4.

You could set these values at design time, but the Properties window only lets you save text values in the Tag property. The code sets them as floating point values so later code can use them in numerical calculations. As you'll see, the code must still cast the values from the generic Object data type that the Tag property has into the float type, but at least it doesn't need to parse textual values.

When you select the File menu's Open command, the following code loads an image file.

// Open a file. private void mnuFileOpen_Click(object sender, EventArgs e) { if (ofdPicture.ShowDialog() == DialogResult.OK) { Bitmap bm = LoadBitmapUnlocked(ofdPicture.FileName); picZoom.ClientSize = new Size(bm.Width, bm.Height); picZoom.Image = bm;


picZoom.Visible = true; } }

This code displays an OpenFileDialog. If you select a file and click Open, the program calls the LoadBitmapUnlocked method to load the image without locking its file. (See the example Load images without locking their files in C# for information on the LoadBitmapUnlocked method.)

When the user selects a scaling menu item, the following code scales the image.

// Scale the image. private void mnuScale_Click(object sender, EventArgs e) { ToolStripMenuItem mnu = sender as ToolStripMenuItem; float scale = (float)mnu.Tag; picZoom.ClientSize = new Size( (int)(scale * picZoom.Image.Width), (int)(scale * picZoom.Image.Height)); CheckMenuItem(mnu); }

This code converts the sender (the menu item clicked) into a ToolStripMenuItem. It gets the item's Tag property and converts it into a numeric scale value.

Next the code sets the client size of the program's PictureBox to the size of the image multiplied by the scale factor. The control automatically assumes the size needed to give it the desired client size plus any borders it may be displaying.

That displays the image at a scaled sizez, but it doesn't actually resize the image. The program does that when it executes the following code to save the resized image.

// Make a bitmap of the correct size. using (Bitmap bm = new Bitmap( picZoom.ClientSize.Width, picZoom.ClientSize.Height)) { // Copy the original image onto the new bitmap. using (Graphics gr = Graphics.FromImage(bm)) { Rectangle source_rect = new Rectangle( 0, 0, picZoom.Image.Width, picZoom.Image.Height); Rectangle dest_rect = new Rectangle( 0, 0, bm.Width, bm.Height); gr.DrawImage(picZoom.Image, dest_rect, source_rect, GraphicsUnit.Pixel); } // Save the bitmap. SaveImage(bm, sfdPicture.FileName); }

The code creates a Bitmap of the correct size (the current size of the PictureBox's client area) to fit the image's current scaled size and creates a Graphics object associated with the Bitmap. It then creates Rectangles representing the source area (the entire image) and the destination area (the scaled area).

The program uses the Graphics object's DrawImage method to draw the image onto the new Bitmap. It finishes by calling the SaveImage method to save the Bitmap in the appropriate format. (See the example Save images with an appropriate format depending on the file name's extension in C# for information about the SaveImage method.)

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.