How to C# Socket Programming using Visual Studio.Net

Learn step by step network programming using C#.Net socket, remoting, WCF and SignalR technology from lots of source code and description. All source code download link available at end of the blog post.

Lossless image resize in C# by keeping same aspect ratio.

Now I am developing an image sharing and Indian social network application (www.Alap.Me) where I need to resize image (JPG for my case). This is a very common situation to all coders and it helps us if we get some readymade method which can serve our purpose. I found basic code of this somewhere from Google and later I modified it as I need and finally developed this code.
This code can resize any image by keeping aspect ratio, so your image always will be same in width vs. height ratio.

How to use it?


This is very easy to use this code; just you need to call it with image file name with full path and maximum height and width. My code automatically detects height or width, which is maximum at your parameter and based on this, code will set another parameter based on aspect ratio and finally resize your image.

ResizeImage(fileLocationWithName, maxWidth, maxHeight);

Complete Code in C#.Net


   public Bitmap ResizeImage(string fileNameWithPath, int maxWidth, int maxHeight)
    {        
        FileStream stream = new FileStream(fileNameWithPath, FileMode.Open);
        Stream streamImage = (Stream)stream;
        Bitmap originalImage = new Bitmap(streamImage);
        int newWidth = originalImage.Width;
        int newHeight = originalImage.Height;
        double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
         if (aspectRatio > 1 && originalImage.Width > maxWidth)
        {            
            newWidth = maxWidth;            newHeight = (int)Math.Round(newWidth / aspectRatio);
        }        
        else 
            if (aspectRatio <= 1 && originalImage.Height > maxHeight)
            {            
                newHeight = maxHeight;            newWidth = (int)Math.Round(newHeight * aspectRatio);
            }         
        Bitmap newImage = new Bitmap(originalImage, newWidth, newHeight);
        Graphics g = Graphics.FromImage(newImage);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

        g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);         

        originalImage.Dispose();        
        stream.Close();        
        stream.Dispose();        
        return newImage;
    }


Complete code in VB.Net:

Public Function ResizeImage(fileNameWithPath As String, maxWidth As Integer, maxHeight As Integer) As Bitmap
 Dim stream As New FileStream(fileNameWithPath, FileMode.Open)
 Dim streamImage As Stream = DirectCast(stream, Stream)
 Dim originalImage As New Bitmap(streamImage)
 Dim newWidth As Integer = originalImage.Width
 Dim newHeight As Integer = originalImage.Height
 Dim aspectRatio As Double = CDbl(originalImage.Width) / CDbl(originalImage.Height)

 If aspectRatio > 1 AndAlso originalImage.Width > maxWidth Then
  newWidth = maxWidth
  newHeight = CInt(Math.Round(newWidth / aspectRatio))
 ElseIf aspectRatio <= 1 AndAlso originalImage.Height > maxHeight Then
  newHeight = maxHeight
  newWidth = CInt(Math.Round(newHeight * aspectRatio))
 End If

 Dim newImage As New Bitmap(originalImage, newWidth, newHeight)

 Dim g As Graphics = Graphics.FromImage(newImage)
 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
 g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height)

 originalImage.Dispose()
 stream.Close()
 stream.Dispose()

 Return newImage
End Function

I have compressed attached picture by using my code and see here difference.
This is after re-size (around 30KB)



This is original size photo (around 5MB)

























I hope this will help you to make easier your coding. Thank you for reading my blog.

Labels: , , , , , , , , ,

leave a response