0
votes

i generate picture according to the size of picture box and set the picture to picture box whose size mode is normal but full image is not showing rather few area of picture is cutting off. i want to generate picture in such a way as a result when i will set the picture on picture box then full image should be display. here is my code by which i generate picture

    new Bitmap _lastSnapshot = new Bitmap(261, 204);
    this.DrawToBitmap((Bitmap)_lastSnapshot, new Rectangle(Point.Empty, ((Bitmap)_lastSnapshot).Size));

261, 204 is size of picture box and picture size mode is normal.i assign the lastSnapshot to picture box after generation but full picture is not displaying.

i got a routine to resize image according to picture box size. it works well but image looks become obscure or unclear.i have to set the picturebox size mode stretch to fill up the image into pic box.

here is the routine i use to resize picture according to picture box size.

public static Image ResizeImage(Image image, Size size, 
    bool preserveAspectRatio = true)
{
    int newWidth;
    int newHeight;
    if (preserveAspectRatio)
    {
        int originalWidth = image.Width;
        int originalHeight = image.Height;
        float percentWidth = (float)size.Width / (float)originalWidth;
        float percentHeight = (float)size.Height / (float)originalHeight;
        float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
        newWidth = (int)(originalWidth * percent);
        newHeight = (int)(originalHeight * percent);
    }
    else
    {
        newWidth = size.Width;
        newHeight = size.Height;
    }
    Image newImage = new Bitmap(newWidth, newHeight);
    using (Graphics graphicsHandle = Graphics.FromImage(newImage))
    {
        graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
    }
    return newImage;
}

call the routine

ResizeImage(value,pictureBox1.Size,true);

can anyone give some advise to generate and resize the picture for fit into picture box with good crystal clear image. thanks

1
Your decision to abandon all adherence to principles of sound writing (such as terse sentence structure, punctuation, and paragraphing) has caused me to abandon all desire of assisting you.Pieter Geerkens

1 Answers

0
votes

If you want to preview of image in picturebox according to picturebox size then: Change PictureBox property SizeMode to Zoom.

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;