Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,
my requirement is upload the image and add to watermark image its working fine but the main image size is 1mb after added the watermark in same image size is 400kb .size is reducing but my requirement size is increasing i am writing this code.....after drawimage method size is reducing please give me a solution....


C#
protected void Button1_Click(object sender, EventArgs e)
   {
       FileName = Guid.NewGuid().ToString();
       FilePath = @"C:\Users\bravemountravi\Desktop\Image\Attachments\" + FileName + "." + "jpg";

       System.Drawing.Image Mainimg = System.Drawing.Image.FromFile(@"C:\Users\travi\Desktop\Image\Attachments\funny-dog-wallpaper-and-picture-p0d-funny-dog-photo-hd-ahhw.jpg");

           System.Drawing.Image img2 = System.Drawing.Image.FromFile(@"C:\Users\travi\Desktop\Image\Images\watermark3.png");

           Graphics g = Graphics.FromImage(Mainimg);



           double _scalefactor = (Mainimg.Width / 4800.0);

           if (_scalefactor <= 0.1)
           {
               _scalefactor = 0.1;
           }
           else    if (_scalefactor <= 0.2)
           {
              _scalefactor = 0.2;
           }

           else if (_scalefactor >= 1)
           {
               _scalefactor = 1.0;
           }


           double _newwidth = (_scalefactor * img2.Width);
           //double _newHeight = ((_newwidth / img2.Width) * (img2.Height));

           double _newHeight = (_scalefactor * img2.Height);


           Bitmap bitimg = WaterMarkScaleImage(img2, Convert.ToInt32(Math.Round(_newwidth, 0)), Convert.ToInt32(Math.Round(_newHeight, 0)));
           System.Drawing.Image scaleimg = (System.Drawing.Image)bitimg;


           int left = Mainimg.Width - scaleimg.Width;
           int top = Mainimg.Height - scaleimg.Height;

         g.DrawImage(scaleimg, left, top, scaleimg.Width, scaleimg.Height);

           g.Dispose();
          Mainimg.Save(FilePath);

   }
   public Bitmap WaterMarkScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
   {
       var newImage = new Bitmap(maxWidth, maxHeight);
       Graphics.FromImage(newImage).DrawImage(image, 0, 0, maxWidth, maxHeight);
       Bitmap bmp = new Bitmap(image);
       return bmp;
   }
Posted
Updated 27-Jul-14 20:08pm
v4

1 solution

When you load and then save a jpg file (even of you do nothing to it) you ate likely to get a smaller file - and a worse image. This is fundamental to how jpg files work: they are a lossy compression method, which reduces the number of pixels by combining them when they have a similar colour.

Try it: read a jpg, save it and repeat 10 times. Compare the original to the final file.

So if you upload a file, edit it, and save it you are very likely to get a smaller file with lower quality.
You could try saving it as a bitmap file or a lossless compression format - but those can also be quite large, and won't be the same size as the original except for bitmaps which are pretty much a fixed size based on the resolution.
 
Share this answer
 

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