0

Resizing an Image file in C#, from those wich are common used at least (bmp, jpg etc etc.)

I found many snippets but not a really complete one. So I'm gonna ask again so who comes here might use a complete file:

This just outputs a file with same width and height.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace PicResize
{
    class Program
    {
        static void Main(string[] args)
        {
            ResizeImage(0, 0, 200, 200);
        }

        public static void ResizeImage(int X1, int Y1, int Width, int Height)
        {

            string fileName = @"C:\testimage.jpg";
            using (Image image = Image.FromFile(fileName))
            {
                using (Graphics graphic = Graphics.FromImage(image))
                {
                    // Crop and resize the image.
                    Rectangle destination = new Rectangle(0, 0, Width, Height);
                    graphic.DrawImage(image, destination, X1, Y1, Width, Height, GraphicsUnit.Pixel);
                }
                image.Save(@"C:\testimagea.jpg");
            }
        }
    }
}

So, since there are no good examples around, How this works? What I need to fix here?

Thanks

Liquid Core
  • 1
  • 6
  • 27
  • 52
  • The words `width` and `height` are very informative – Sayse Jul 19 '13 at 15:50
  • Since I'm asking to resize an image, and therefore to write a new image on a new canvas/file, I guess that an origin point 0,0 in the canvas and the new Width and Height, describe perfectly what they are used for. – Liquid Core Jul 19 '13 at 15:56
  • > "Questions concerning problems with code you've written must >describe the specific problem and include valid code to reproduce it. > See SSCCE.org for guidance." – DGibbs, p.s.w.g, ryan1234, CanSpice WHAT? You have: A) My code B) A specific question (WHAT TO FIX, WHAT IS NOT WORKING?) C) Valid code to reproduce. Instead of going in a modding spree for some virtual reputation, try to be constructive – Liquid Core Jul 22 '13 at 07:31
  • I didn't vote to close (I think) but I'm sure this is why it would have been.. You haven't included your code, you've included what seems to be from a different online source. Your specific question sounds like how can I use this example to suit my needs, without any obvious attempt by yourself to do so, and for C, see My first point. – Sayse Jul 22 '13 at 07:38
  • Well, by using commons sense and by being objective, ANYONE could conclude that even if I peeked at online snippets to see how it's done (and anyone does), that is a REALLY REALLY REALLY generic code snippet I posted. If you want to keep it simple and fast, that's what everyone would do. Since I'm doing such a "simple" task, the code is the same for everyone and this is the common way of doing it, so you shouldn't expect different code from anyone else asking the same question. It's like someone asks how a for loop works and saying "hey you copied your for code from somewhere else". ;) – Liquid Core Jul 22 '13 at 08:00

2 Answers2

5

You can do this:

        public void ResizeImage(string fileName, int width, int height)
        {
            using (Image image = Image.FromFile(fileName))
            {
                new Bitmap(image, width, height).Save(fileName);
            }
        }

If its a new file just replace with this or with a custom path of your choosing:

new Bitmap(image, width, height).Save(fileName.Insert(fileName.LastIndexOf('.'),"A"));
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
  • that one worked, thanks! there is any way to polish the image a bit? I saw some code about image properties when saving, but I can't find them in the Bitmap Object properties. Just an extra, if you know – Liquid Core Jul 22 '13 at 08:10
  • you mean changing the pixels or try to save it with best quality? – terrybozzio Jul 22 '13 at 12:53
  • Trying to save it resized with the least loss of quality possible For example, this is some of the code I found: http://stackoverflow.com/questions/87753/resizing-an-image-without-losing-any-quality – Liquid Core Jul 22 '13 at 12:55
  • 1
    and that question is right,HighQualityBicubic is your best choice here,adjust that code inside the using statment here and then save it. – terrybozzio Jul 22 '13 at 13:19
  • 1
    take a look for example a this article and check the comments because there are some tips if you dont know how to use in your case - http://programanddesign.com/cs/high-quality-image-resize-c/ – terrybozzio Jul 22 '13 at 13:22
3

The problem with your example code is that you are opening the image, and simple drawing on to that image, without actually changing the size.

What you can do is create a new Bitmap based on an original image and supply it with a new size. This function should work for you:

public void ResizeImage(string fileName, int width, int height)
{
    using (Image image = Image.FromFile(fileName))
    {
        using (Image newImage = new Bitmap(image, width, height))
        {
            //must dispose the original image to free up the file ready
            //for re-write, otherwise saving will throw an error
            image.Dispose();
            newImage.Save(fileName);
        }
    }
}
musefan
  • 47,875
  • 21
  • 135
  • 185