C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Resize your image through C# Code


Posted Date: 19 Oct 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Kritika YadavMember Level: Gold    
Rating: 1 out of 5Points: 10



This is a code in C# to resize an image. it takes two inputs as target image size and the image in byte format and it'll will return target sized image.

Some terms used in the code:-
Format24bppRgb - It specifies that formate is 24 bit per pixel and for each colour that is red, green and blue 8 bits are used



using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class ImageClass
{

private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);//User defined function to calculate image dimensions
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;//set rendering quality of canvas
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;//set interpolation mode to HighQualityBiubic
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));//drow image in canvas with new size and with specific starting location
MemoryStream m = new MemoryStream();
newImage.Save(m, ImageFormat.Jpeg);//define image formate in which it is to be shown
return m.GetBuffer();//return image as byte array
}
}
}
}

private static Size CalculateDimensions(Size oldSize, int targetSize)
{
Size newSize = new Size();

//change the size according to the widh and height
if (oldSize.Height > oldSize.Width)
{
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
newSize.Height = targetSize;
}
else
{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
}
return newSize;
}
}



Responses

Author: David Kearfott    18 Nov 2009Member Level: Bronze   Points : 1
This is terrific. Thanks for posting.
Quick question: my resized images appear to have a thin border around each one. Is there any way to remove this?

Thanks,
david


Author: David Kearfott    18 Nov 2009Member Level: Bronze   Points : 2
Hi there,
I was able to fix my own problem by following the recommendations in this post: http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx which suggested to provide some ImageAttributes. This meant I had to use a different signature when calling DrawImage, as follows (I'm now passing in the origWidth and origHeight):

System.Drawing.Imaging.ImageAttributes attr =
new System.Drawing.Imaging.ImageAttributes();

attr.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);

canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize), 0, 0, origWidth, origHeight, GraphicsUnit.Pixel, attr);

attr.Dispose();

Thanks,
david


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Resize image  .  Resize  .  Image  .  Canvas  .  Bytes array  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: WPF - Binding DataTable to a WPF Datagrid
Previous Resource: List files in any folder
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use