by Gehan Fernando (47 Submissions)
Category: Graphics
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Sat 5th March 2011
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Resize your image without loosing image quality in C#.net - Bug Fixed Code.
API Declarations
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
// Developer :- Gehan Fernando
// Date :- 26-April-2011
// Description :- Resize your image without loosing image quality as well
// save your new image according to your quality range.
// Contact :- [email protected], +94772269625 (M) ...... Sri Lanka.
{
public partial class FormResize : Form
{
public FormResize()
{
InitializeComponent();
}
private void FormResize_Load(object sender, EventArgs e)
{
trackBarImageQuality.Value = 100;
}
private void buttonBrowseImage_Click(object sender, EventArgs e)
{
this.GetFileName();
}
private void buttonProcess_Click(object sender, EventArgs e)
{
try
{
String filename = String.Concat(@"E:\Temp Data\NewImage", "_",
DateTime.Now.ToString("ddMMyyhhmmss"),
".jpeg");
this.ResizeImage(Image.FromFile(textBoxImagePath.Text),
new Size((int)numericUpDownWidth.Value,
(int)numericUpDownHeight.Value), filename);
pictureBoxResize.Image = Image.FromFile(filename);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonReset_Click(object sender, EventArgs e)
{
textBoxImagePath.ResetText();
trackBarImageQuality.Value = 100;
numericUpDownWidth.Value = pictureBoxResize.Width;
numericUpDownHeight.Value = pictureBoxResize.Height;
pictureBoxResize.Image = null;
}
private void textBoxImagePath_Enter(object sender, EventArgs e)
{
textBoxImagePath.SelectAll();
}
private void textBoxImagePath_TextChanged(object sender, EventArgs e)
{
if (textBoxImagePath.Text.Trim().Length == 0)
{
buttonProcess.Enabled = false;
return;
}
if (File.Exists(textBoxImagePath.Text))
buttonProcess.Enabled = true;
else
buttonProcess.Enabled = false;
}
private void trackBarImageQuality_Scroll(object sender, EventArgs e)
{
labelImageQualityValue.Text = String.Concat(trackBarImageQuality.Value, "%");
}
private void GetFileName()
{
OpenFileDialog openFileDialogImageResize = new OpenFileDialog()
{
AddExtension = false,
AutoUpgradeEnabled = true,
CheckFileExists = true,
CheckPathExists = true,
Filter = "Image Files|*.jpeg;*.jpg;*.bmp",
FilterIndex = 0,
Multiselect = false,
ReadOnlyChecked = false,
RestoreDirectory = false,
ShowHelp = false,
ShowReadOnly = false,
SupportMultiDottedExtensions = true,
Title = "Select image to resize",
ValidateNames = true
};
if (openFileDialogImageResize.ShowDialog() == DialogResult.OK)
textBoxImagePath.Text = openFileDialogImageResize.FileName;
}
private void ResizeImage(Image OrginalImage, Size ImageSize,String newfilepath)
{
int sourceWidth = OrginalImage.Width;
int sourceHeight = OrginalImage.Height;
float ratio;
float currentWidth;
float currentHeight;
currentWidth = ((float)ImageSize.Width / (float)sourceWidth);
currentHeight = ((float)ImageSize.Height / (float)sourceHeight);
if (currentHeight < currentWidth)
ratio = currentHeight;
else
ratio = currentWidth;
int newWidth = (int)(sourceWidth * ratio);
int newHeight = (int)(sourceHeight * ratio);
Bitmap img = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage((Image)img);
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality = CompositingQuality.HighQuality;
if (newWidth >= OrginalImage.Width && newHeight >= OrginalImage.Height)
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
else
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(OrginalImage, 0, 0, newWidth, newHeight);
Image newimage = (Image)img;
this.SaveImage(newfilepath, (Bitmap)newimage, trackBarImageQuality.Value);
g.Dispose();
}
private void SaveImage(string SaveTo, Bitmap ImgFile, long Quality)
{
Bitmap newImage;
ImageCodecInfo imageCodecInfo;
Encoder encoder;
EncoderParameter encoderParameter;
EncoderParameters encoderParameters;
// Create a Bitmap object based on a BMP file.
newImage = new Bitmap(ImgFile);
// Get an ImageCodecInfo object that represents the JPEG codec.
imageCodecInfo = GetEncoderInfo(@"image/jpeg");
// for the Quality parameter category.
encoder = Encoder.Quality;
// EncoderParameter object in the array.
encoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with quality level 25.
encoderParameter = new EncoderParameter(encoder, Quality);
encoderParameters.Param[0] = encoderParameter;
using (MemoryStream mstream = new MemoryStream())
{
using (FileStream fstream = new FileStream(SaveTo, FileMode.Create, FileAccess.Write))
{
newImage.Save(mstream, imageCodecInfo, encoderParameters);
Byte[] buff = mstream.ToArray();
fstream.Write(buff, 0, buff.Length);
newImage.Dispose();
mstream.Flush();
fstream.Flush();
};
};
}
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}
}
No comments have been posted about Resize your image without loosing image quality in C#.net - Bug Fixed Code.. Why not be the first to post a comment about Resize your image without loosing image quality in C#.net - Bug Fixed Code..