Ran
Ran

Reputation: 662

How to know what is the length of an edge in a resized image?

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

Upvotes: 0

Views: 74

Answers (1)

PhillipH
PhillipH

Reputation: 6222

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.

Upvotes: 1

Related Questions