3

I'm making a cropping tool for images and I can't for the life of me figure out why it's producing the images that it's producing...

I'm following the accepted answer here, but its still being weird... Here's my code:

public void Crop(
    string FileName,
    Crop Crop) {
    using (Bitmap Source = ((Bitmap)Image.FromFile(FileName))) {
        using (Bitmap Target = new Bitmap(Crop.Width, Crop.Height)) {
            using (Graphics Graphics = Graphics.FromImage(Target)) {
                Graphics.DrawImage(Source, new Rectangle(new Point(Crop.Left, Crop.Top), new Size(Crop.Width, Crop.Height)), new Rectangle(new Point(0, 0), new Size(Target.Width, Target.Height)), GraphicsUnit.Pixel);
            };

            Target.Save((FileName + ".temp"), JpegCodecInfo, HighQualityEncoder);
        };
    };


    this.NormalizeFileName(FileName);
}

Please help me. I'm attaching an image of what I'm getting...alt text

UPDATE

For @Aaronontheweb, here's the Crop class and how it's populated:

public class Crop {
    [Required]
    public short Height { get; set; }

    [Required]
    public short Left { get; set; }

    [Required]
    public short Top { get; set; }

    [Required]
    public short Width { get; set; }
}

And the jQuery that populates it:

$("#Image input:submit").bind("click", function () {
    $("#Crop\\.Height").val(Crop.height());
    $("#Crop\\.Left").val(Crop.position().left);
    $("#Crop\\.Top").val(Crop.position().top);
    $("#Crop\\.Width").val(Crop.width());
});

UPDATE 2

Never mind, I got it. I took a nap after asking my question just to clear my head and when I looked at it again after, I decided to switch the two rectangles and see what happens. Well, guess what, that fixed it.

At this point I would have to say that the names given in the API documentation are deceptive. For example the docs refer to the output image as source and the input image as display. Perhaps the API should be updated to have better naming?

Community
  • 1
  • 1
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
  • 1
    I'm not sure but I think you have your rectangles backwards... Drawimage(image, destRect, sourceRect,...) check the API doc... – Jaime Dec 14 '10 at 05:27
  • Could you post the source of your Crop class? It looks like an X/Y inversion error offhand (meaning, you swapped an X with a Y somewhere in your .DrawImage function call) – Aaronontheweb Dec 14 '10 at 05:34
  • @Aaronontheweb, I've updated my post with the information you wanted. – Gup3rSuR4c Dec 14 '10 at 05:39
  • I have a similar issue with a project of mine, but here is the weird part...this happens after the user crops about 10 images or so...the user closes the app, reopens and it works....still looking for a solution. – Saif Khan Dec 14 '10 at 05:42
  • You might want to check out http://bobpowell.net, the GDI+ section has great tuts on many GDI tasks above and beyond cropping! Disclaimer: No relation, I just dig his tuts, they're great reference material. – invert Dec 14 '10 at 07:33

1 Answers1

3

In the following line:

Graphics.DrawImage(Source, new Rectangle(new Point(Crop.Left, Crop.Top), new Size(Crop.Width, Crop.Height)), new Rectangle(new Point(0, 0), new Size(Target.Width, Target.Height)), GraphicsUnit.Pixel);

Make sure the Crop.Left and Crop.Top is set to 0, otherwise it will start setting the pixels at the weird offset you are getting. You can also just replace it with the following line to test:

Graphics.DrawImage(Source, new Rectangle(new Point(0,0), new Size(Crop.Width, Crop.Height)), new Rectangle(new Point(0, 0), new Size(Target.Width, Target.Height)), GraphicsUnit.Pixel);

TBohnen.jnr
  • 5,117
  • 1
  • 19
  • 26
  • 1
    If he were to use .DrawImage that way then all he would be able to crop are images that begin at the top left corner of his source images. The offset is necessary in order to actually have cropping behavior. – Aaronontheweb Dec 14 '10 at 05:37
  • Exactly what I was thinking, it completely defeats the effort of cropping the image... – Gup3rSuR4c Dec 14 '10 at 05:40
  • I see your point, but the first rectangle is the destination rectangle and it will start drawing at that point in the destination rectangle, so should he not rather set the offset on the second rectangle then? – TBohnen.jnr Dec 14 '10 at 05:46
  • you're right, I switched the two rectangles and it worked, but I have to say, the API naming is not exactly clear... – Gup3rSuR4c Dec 14 '10 at 05:48