Friday, 27 September 2013

Ensuring same Image format after creating a new Bitmap

Ensuring same Image format after creating a new Bitmap

I am currently working on some image re size task.
here are my method of resize,
private Bitmap ReSizeImageKeepingAspectRation(string imageFilePath, double
scale)
{
Bitmap resizedImage = null;
if (File.Exists(imageFilePath) && scale != 0)
{
Image originalImage = null;
FileStream imageStream = null;
try
{
imageStream = new FileStream(imageFilePath,
FileMode.Open);
originalImage = Image.FromStream(imageStream);
Size newImageSize = new Size((int)(originalImage.Width
* scale), (int)(originalImage.Height * scale));
resizedImage = new Bitmap(newImageSize.Width,
newImageSize.Height);
Graphics g = Graphics.FromImage(resizedImage);
g.DrawImage(originalImage, 0, 0, newImageSize.Width,
newImageSize.Height);
g.Dispose();
g = null;
}
catch (Exception ex)
{
}
finally
{
if (imageStream != null)
{
imageStream.Close();
imageStream.Dispose();
}
}
}
return resizedImage;
}
But you can see in this line
resizedImage = new Bitmap(newImageSize.Width, newImageSize.Height);
i unable to provide image format. that's why i am getting the inputted
image format is different from the newly created returned image format.
So my question is it possible to remain the same image format for returned
image as inputted image?

No comments:

Post a Comment