Click here to Skip to main content
15,908,581 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to reduce the image of size using asp.net, without lose of image quality
Posted

Bitmap was just saved with a smaller resolution with a '.jpg' slapped onto it, and not compressed accordingly. The following code now works.

Bitmap tempBmp = new Bitmap(filePath);
Bitmap bmp = new Bitmap(tempBmp, 807, 605);

bmp.Save(
@"C:\NewPicFolder\Pic" + count + ".jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
count++;
}

See one more solution on the following link

C#: Image resize, convert, and save
 
Share this answer
 
Here is a simple program to help you to reduce the size of image in pixel.
Please give it a look

http://iknowledgeboy.blogspot.in/2014/03/c-creating-thumbnail-of-large-image-by.html
 
Share this answer
 
Found this method on one of the threads of CodeProject[^]
public static void ResizeImage(string image, string Okey, string key, int width, int height, string newimagename)
    {
        System.Drawing.Image oImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image));
 
        System.Drawing.Image oThumbNail = new System.Drawing.Bitmap(width, height);//, System.Drawing.Imaging.PixelFormat.Format24bppRgb

        System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(oThumbNail);
 
        oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
 
        //set smoothing mode to high quality
        oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //set the interpolation mode
        oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        //set the offset mode
        oGraphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
 
        System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, width, height);
 
        oGraphic.DrawImage(oImg, oRectangle);
 
        if (newimagename == "")
        {
            if (image.Substring(image.LastIndexOf(".")) != ".png")
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Jpeg);
            else
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Png);
        }
        else
        {
            if (newimagename.Substring(newimagename.LastIndexOf(".")) != ".png")
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Jpeg);
            else
                oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Png);
        }
        oImg.Dispose();
    }

You might be interested in this as well : http://www.aspdotnet-suresh.com/2011/05/how-to-resize-size-image-without-losing.html[^]
http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx[^]
http://weblogs.asp.net/bleroy/archive/2010/05/03/the-fastest-way-to-resize-images-from-asp-net-and-it-s-more-supported-ish.aspx[^]
http://www.4guysfromrolla.com/articles/012203-1.aspx[^]

-KR
 
Share this answer
 
protected void btnsave_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string targetPath = Server.MapPath("Images/" + filename);
Stream strm = fileupload1.PostedFile.InputStream;
var targetFile = targetPath;
//Based on scalefactor image size will vary
GenerateThumbnails(0.5, strm, targetFile);
///////////
}

private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)
{
using (var image = Image.FromStream(sourcePath))
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath, image.RawFormat);
}

Also see below link
http://www.aspdotnet-suresh.com/2011/05/how-to-resize-size-image-without-losing.html[^]
 
Share this answer
 
v2
If the image has reasonable quality and compressed reasonably well, you cannot keep its quality if you reduce the size. Note, that you did not really explain what do you mean by size: size in pixels or size of image file in bytes. In both meaning, the problem is pretty much hopeless. Well, if you are talking about size of file, and the file was not compressed or poorly compressed, you usually can compress it better and hence keep subjective quality reasonably well.

As the question is not certain enough, I cannot provide more detail, but my past answers could help you:
resize image in vb.net[^],
Read Big Tiff and JPEG files (>(23000 x 23000) pix) in a stream. And display part of it to the screen in realtime.[^].

—SA
 
Share this answer
 
Here's a tip, create a
C#
<div></div>

for your image and for example you want your image size to be 20px x 20px
then set your div size to that.

Then use photoshop to resize the image.
 
Share this answer
 
Hi,

I'll go with the code which was given by Krinal Rohit. But be adviced that some PNG images cannot be re-sized with convesional methods described above. Though it good for beginners to learn the basics of drawing using Asp.Net namespaces like System.Drawing and System.Drawing.Imaging.

There are online tools like TinyPng.com and Online Image Re-sizer with which you can get the desired result. tinypng.com can reduce the size of a PNG image drastically.

Arun
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900