Hi ashishk,
Try below code gives you the required combination of quality and size.
HTML
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button Text="Upload" runat="server" OnClick="OnUpload" />
Namespaces
C#
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
VB.Net
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Code
C#
protected void OnUpload(object sender, EventArgs e)
{
HttpPostedFile files = fuUpload.PostedFile;
string imgName = Path.GetFileName(files.FileName);
string imgPath1 = "~/ThumbPhotos/" + imgName;
int widthC = 800;
int heightC = 800;
Stream streamC = files.InputStream;
using (Bitmap imageC = new Bitmap(streamC))
{
using (Bitmap targetC = new Bitmap(widthC, heightC, PixelFormat.Format24bppRgb))
{
using (Graphics graphicC = Graphics.FromImage(targetC))
{
graphicC.SmoothingMode = SmoothingMode.AntiAlias;
graphicC.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicC.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicC.CompositingQuality = CompositingQuality.HighSpeed;
graphicC.CompositingMode = CompositingMode.SourceCopy;
graphicC.DrawImage(imageC, 0, 0, widthC, heightC);
targetC.Save(Server.MapPath(imgPath1), ImageFormat.Jpeg);
}
}
}
}
VB.Net
Protected Sub OnUpload(ByVal sender As Object, ByVal e As EventArgs)
Dim files As HttpPostedFile = fuUpload.PostedFile
Dim imgName As String = Path.GetFileName(files.FileName)
Dim imgPath1 As String = "~/ThumbPhotos/" & imgName
Dim widthC As Integer = 800
Dim heightC As Integer = 800
Dim streamC As Stream = files.InputStream
Using imageC As Bitmap = New Bitmap(streamC)
Using targetC As Bitmap = New Bitmap(widthC, heightC, PixelFormat.Format24bppRgb)
Using graphicC As Graphics = Graphics.FromImage(targetC)
graphicC.SmoothingMode = SmoothingMode.AntiAlias
graphicC.InterpolationMode = InterpolationMode.HighQualityBicubic
graphicC.PixelOffsetMode = PixelOffsetMode.HighQuality
graphicC.CompositingQuality = CompositingQuality.HighSpeed
graphicC.CompositingMode = CompositingMode.SourceCopy
graphicC.DrawImage(imageC, 0, 0, widthC, heightC)
targetC.Save(Server.MapPath(imgPath1), ImageFormat.Jpeg)
End Using
End Using
End Using
End Sub