Hi ahmadsubuhanl...,
Use System.Drawing to compress the image. You need to set the Encoder.Quality of EncoderParameter class.
Refer below example.
HTML
<asp:FileUpload ID="fuImage" runat="server" />
<asp:Button Text="Resize" ID="btnResize" runat="server" OnClick="OnResize" />
Namespaces
C#
using System.Drawing.Imaging;
VB.Net
Imports System.Drawing.Imaging
Code
C#
protected void OnResize(object sender, EventArgs e)
{
System.Drawing.Image inputImage = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
this.SaveJpeg(inputImage, Server.MapPath("~/Output.jpg"), 50);
}
public void SaveJpeg(System.Drawing.Image inputImage, string path, int quality)
{
EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, quality);
ImageCodecInfo imageCodecInfo = this.GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = encoderParameter;
inputImage.Save(path, imageCodecInfo, encoderParams);
}
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i <= codecs.Length - 1; i++)
{
if (codecs[i].MimeType == mimeType)
{
return codecs[i];
}
}
return null;
}
VB.Net
Protected Sub OnResize(sender As Object, e As EventArgs)
Dim inputImage As System.Drawing.Image = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream)
Me.SaveJpeg(inputImage, Server.MapPath("~/Output.jpg"), 50)
End Sub
Public Sub SaveJpeg(inputImage As System.Drawing.Image, path As String, quality As Integer)
Dim encoderParameter As EncoderParameter = New EncoderParameter(Encoder.Quality, quality)
Dim imageCodecInfo As ImageCodecInfo = Me.GetEncoderInfo("image/jpeg")
Dim encoderParams As EncoderParameters = New EncoderParameters(1)
encoderParams.Param(0) = encoderParameter
inputImage.Save(path, imageCodecInfo, encoderParams)
End Sub
Private Function GetEncoderInfo(mimeType As String) As ImageCodecInfo
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
For i As Integer = 0 To codecs.Length - 1
If codecs(i).MimeType = mimeType Then
Return codecs(i)
End If
Next
Return Nothing
End Function
Screenshot
Compressed File
If you want to resize the Image refer below link.
Resize image and save into Database with Binary format using C# and VB.Net in ASP.Net