0

I am Resizing a Image and add watermark to image. And Return the image. Here is my code.

Image resizedImage = ResizeImage(original6, new Size(500, 375));

Re size function:

public static System.Drawing.Image ResizeImage(System.Drawing.Image image, Size size)
{
    int newWidth;
    int newHeight;
    if (true)
    {
        int originalWidth = image.Width;
        int originalHeight = image.Height;
        float percentWidth = (float)size.Width / (float)originalWidth;
        float percentHeight = (float)size.Height / (float)originalHeight;
        float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
        newWidth = (int)(originalWidth * percent);
        newHeight = (int)(originalHeight * percent);
    }
    else
    {
        newWidth = size.Width;
        newHeight = size.Height;
    }
    System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
    using (Graphics graphicsHandle = Graphics.FromImage(newImage))
    {
        graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
    }


    System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(newImage, size.Width, size.Height);// create bitmap with same size of Actual image
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);

    SolidBrush brush = new SolidBrush(Color.FromArgb(113, 255, 255, 255));
    //Adding watermark text on image
    g.DrawString("My watermark", new Font("Arial", 16, FontStyle.Bold), brush, 5, 100);

    return bitmapimage;
}

I am retrieve the new re sized image with watermark and going to save as new image file.

resized6.Save(Server.MapPath(sSavePath + ownerRef + "Pic6v2" + ".jpg"));

This is working fine. However I can't control the file size. When my original JPG is only 45kb but when my new re-sized image is 500kb. How can I reduce the file size.? info: original resolution (400x300 px) and new image (500x375px)

JayC
  • 7,053
  • 2
  • 25
  • 41
devan
  • 1,643
  • 8
  • 37
  • 62

2 Answers2

1

I don't remember this off the top of my head, but file size generally has to do with JPEG quality settings. You also need to make sure it's saved as an actual jpg, not a bitmap, which I don't see that you're doing..

See also: C# Simple Image Resize : File Size Not Shrinking

JPEG Quality settings: http://msdn.microsoft.com/en-us/library/bb882583.aspx

Community
  • 1
  • 1
JayC
  • 7,053
  • 2
  • 25
  • 41
0

You may be able to change the quality of the JPEG to get a smaller file size. See

http://msdn.microsoft.com/en-us/library/bb882583.aspx

also

Quality of a saved JPG in C#

Community
  • 1
  • 1
user1888773
  • 144
  • 2