0

Lets say I want to click on an image and enlarge the part above where I clicked. I do that with the simplest code, but at the bottom of the image there is an annoying edge because of the enlargement.

There is no Graphics involved, so I can't use SetWrapMode(WrapMode.TileFlipXY); or graphic.CompositingMode = CompositingMode.SourceCopy;

I can crop the annoying edge part (its not important) but I don't know what is its height.

How can I know what is its height so I can crop it? Better yet - is there a better method to enlarge the image (I need a method that returns a bitmap so I can save it later)?

The simple code:

private void button1_Click(object sender, EventArgs e)
{   
    try
    {   
        Bitmap img = new Bitmap(imageLoc);
        Rectangle cropArea = new Rectangle(0, 0, img.Width, yLoc);
        Bitmap bmpImage = new Bitmap(img);
        Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);

        pictureBox1.Image = resizeImage(bmpCrop, new Size(bmpCrop.Width, newHeight));
    }
    catch
    {
    }
}

public static Bitmap resizeImage(Image imgToResize, Size size)
{
    return new Bitmap(imgToResize, size);
}

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    xLoc = e.X;
    yLoc = e.Y;
}

The annoying edge (look at the bottom): enter image description here

Ran
  • 657
  • 2
  • 13
  • 29
  • If I understand right, your original graphic has an area at the bottom that is white or transparent. To avoid the problem you're seeing you should probably crop off this area at the bottom before you do your resize operation. You should be able to determine how large this area is by examining the pixels in the image. – RenniePet Mar 07 '15 at 12:52
  • No. My original image doesn't have an area in white. The white appeared after I resized the image. – Ran Mar 07 '15 at 12:57
  • That sound very strange to me. Does the image have opacity values for the pixels, i.e., is it 32 bits per pixel or 24 bits per pixel? – RenniePet Mar 07 '15 at 13:03
  • I don't know. The original image is: https://www.flickr.com/photos/ikkoskinen/329634175/ What I did was to resize the sky (the blue part of a few pixels above the building). – Ran Mar 07 '15 at 13:07
  • OK, the original is a .jpg, which does not normally have an opacity channel. Did you download the original size, 1500 x 1125, or a version resized by Flickr? Just for the heck of it, could you try downloading the original size (if you haven't done that already) and convert it to .png before you fetch it into your program? You can use any graphics editing program to do the conversion - I favor Gimp, but even Windows Paint should be able to do it. (This is because I don't really trust .jpg images to not contain compression artifacts.) – RenniePet Mar 07 '15 at 13:17
  • Even in png I have the same problem. – Ran Mar 07 '15 at 13:58
  • OK, sorry, I'm not really helping you. – RenniePet Mar 07 '15 at 14:27

1 Answers1

1

I think this is casued by the resizing algorithm using a bicubic or linear interpolation. These alhgorithms calculate pixels value after resizing by sampling the values of the surrounding pixels and interpolating them. In the case of the original edge pixels, they dont have a value for the pixel below them, so tend towards transparency (i.e. get lighter) since the average of the surrounding pixels takes into account that their next pixel down is missing.

To eliminate sizing artefacts like this you probably need to investigate resizing using the Graphics object and the various quality and algorithm settings available through that.

PhillipH
  • 6,182
  • 1
  • 15
  • 25
  • Believe me, I researched graphic object to no avail (I built an elaborate program by researching. This is my final bug). I'm asking you a question, you can't answer me "investigate". – Ran Mar 07 '15 at 14:42
  • Yes, but you dismissed using Graphics object in your question - but the answer is "use the graphics object". Quick research will show answers like this http://stackoverflow.com/questions/4772273/interpolationmode-highqualitybicubic-introducing-artefacts-on-edge-of-resized-im which illustrate the interpolation mode artefacts you are getting, and provide the solutions. If you question is "I am not going to use GDI+ graphics object - how can I get rid of this artefact" then the answer is "you cant". – PhillipH Mar 08 '15 at 11:49