About Author
|
 |
Chintankumar Patel
09 Jun, 2008
|
 |
Contact Me
|
Working as a Technical Consultant for Conchango.
Having experience in to IT from 7+ years and working on Microsoft Technologies
|
Tags
|
|
.Net , .Net Framework 2.0 , .Net Graphics , Asp.Net , Boot , C# , Class Library , Coding Standards , Convert File System , Database , DATEDIFF , DATEDIFF Function , Disk Management , Embedded Resources , EnableEventValidation , EnableViewState , EncoderParameter , FAT32 to NTFS , Form Authentication , HDD , High Quality Thumbnail , Intellisense , Linux , Microsoft Access , Microsoft's SQL , Partition , ReadyBoost , ReSharper , Resize Image , Resize Partition , Security , SQLDataReader , SuperFetch , USB , Usb device not recognized , Vista , Visual Studio 2005 , What's New , Windows , Windows Application , Windows Errors , Windows Vista , Windows XP , YouTube
|
|
Resizing Images to match it's Scale in C# .Net
|
Mon, 18 Aug, 2008
|
|
|
Use the following code to generate the scaled Image which will be scaled to given PictureBox.... In this you can change the size of PictureBox and see how resizing works..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace ImageTest
{
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Button btnLoadImage;
private PictureBox pictureBox1;
private ColorDialog colorDialog1;
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void btnLoadImage_Click(object sender, EventArgs e)
{
using (OpenFileDialog fd = new OpenFileDialog())
{
if (fd.ShowDialog() == DialogResult.OK)
{
try
{
using (Image img = Image.FromFile(fd.FileName))
{
ProcessImage(img);
}
}
catch (Exception ex)
{
MessageBox.Show("Error processing Image File" + Environment.NewLine + "Please check that file is valid image");
}
}
}
}
private void ProcessImage(Image img)
{
Rectangle newRect = GetScaledRectangle(img, pictureBox1.ClientRectangle);
pictureBox1.Image = GetResizedImage(img, newRect);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.btnLoadImage = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// btnLoadImage
//
this.btnLoadImage.Location = new System.Drawing.Point(377, 110);
this.btnLoadImage.Name = "btnLoadImage";
this.btnLoadImage.Size = new System.Drawing.Size(75, 23);
this.btnLoadImage.TabIndex = 0;
this.btnLoadImage.Text = "Load Image";
this.btnLoadImage.UseVisualStyleBackColor = true;
this.btnLoadImage.Click += new System.EventHandler(this.btnLoadImage_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.AppWorkspace;
this.pictureBox1.Location = new System.Drawing.Point(29, 46);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(200, 200);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(514, 366);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.btnLoadImage);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
public static Rectangle GetScaledRectangle(Image img, Rectangle thumbRect)
{
if (img.Width < thumbRect.Width && img.Height < thumbRect.Height)
return new Rectangle(thumbRect.X + ((thumbRect.Width - img.Width) / 2), thumbRect.Y + ((thumbRect.Height - img.Height) / 2), img.Width, img.Height);
int sourceWidth = img.Width;
int sourceHeight = img.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)thumbRect.Width / (float)sourceWidth);
nPercentH = ((float)thumbRect.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
if (destWidth.Equals(0))
destWidth = 1;
if (destHeight.Equals(0))
destHeight = 1;
Rectangle retRect = new Rectangle(thumbRect.X, thumbRect.Y, destWidth, destHeight);
if (retRect.Height < thumbRect.Height)
retRect.Y = retRect.Y + Convert.ToInt32(((float)thumbRect.Height - (float)retRect.Height) / (float)2);
if (retRect.Width < thumbRect.Width)
retRect.X = retRect.X + Convert.ToInt32(((float)thumbRect.Width - (float)retRect.Width) / (float)2);
return retRect;
}
public static Image GetResizedImage(Image img, Rectangle rect)
{
Bitmap b = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, 0, 0, rect.Width, rect.Height);
g.Dispose();
try
{
return (Image)b.Clone();
}
finally
{
b.Dispose();
b = null;
g = null;
}
}
}
}
|
Resizing Images to match it's Scale in C# .Net
|
|
Comments
|
|
Re : Resizing Images to match it's Scale in C# .Net
Wed, 24 Sep, 2008 - 15:47:38 by Olav Alexander Mjelde
|
|
| |
Hi,
I have made my own C# .net (express 2008) image-resizer.
If you are interested in co-developing it, please email me.
I have already made functions: * file-browser * image-resize, keeping aspect ratio * folder-exists function, creating missing folders * archive function, moves the original file to archive. If file is already in archive, rename old archived file and move the srcImage. ++
It's not very advanced yet and all the code is in form1... So it needs more work.
I would like to create a sourceforge project, where a team of contributors develop tools which most people need. eg. like a batch resize app, a filder-diff app with two-way synchronization ( I have also started this app ( it is also in early developtment stages )).
|
| |
|
Re : Resizing Images to match it's Scale in C# .Net
Thu, 16 Oct, 2008 - 15:26:43 by sindhu tiwari
|
|
| |
Hi Chintan Kumar Patel Thanks for the Tutorial or code .. I am developing a software which is utilised to make calendars and photo books I have used Your code for resizing the images My main aim was to shrink the pics and display in a list kind of format Anyway Great Code God Bless You and me Sindhu tiwari
|
| |
|
Re : Resizing Images to match it's Scale in C# .Net
Sat, 18 Oct, 2008 - 18:47:03 by John
|
|
| |
Hi,
Thank you for great code. It work very well.
We just want to ask you this:
In the method: GetScaledRectangle(Image img, Rectangle thumbRect)
What is the Rectangle thumbRect ?
Maybe you can give us some explaination about the algorithm you used ?
Thank you in advance.
Best regards
John
|
| |
|
Re : Resizing Images to match it's Scale in C# .Net
Sun, 19 Oct, 2008 - 09:24:18 by Chintankumar Patel
|
|
| |
Hi John,
Rectangle thumbRect tells the method that in which size of rectangle you want to fit the image so Method will return new Rectangle with maintaining the ratio of image.
Then after new resized image can be retrieved by passing new Rectangle to method GetResizedImage()
Hi Sindhu,
Thanks for using this code in to your application :)
|
| |
|
Re : Resizing Images to match it's Scale in C# .Net
Wed, 29 Oct, 2008 - 05:29:44 by dekpitado
|
|
| |
Vientos ijo, se ve que pinta bien el codigo, lo voi a probar a ver si va bien y si me puede servir... :D
|
| |
|
|
|
|