0

我根据图片框的大小生成图片,并将图片设置为大小模式正常但完整图像不显示的图片框,图片的一小部分区域被切断。我想以这样的方式生成图片,因此当我将图片设置在图片框上时,应该显示完整的图像。这是我生成图片的代码

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

261, 204是图片框大小,图片大小模式正常。我在生成后将lastSnapshot分配给图片框,但未显示完整图片。

我有一个根据图片框大小调整图像大小的例程。它运作良好,但图像看起来变得模糊或不清晰。我必须设置图片框大小模式拉伸以将图像填充到图片框中。

这是我用来根据图片框大小调整图片大小的例程。

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;
}

调用例程

ResizeImage(value,pictureBox1.Size,true);

任何人都可以给出一些建议来生成和调整图片大小以适应具有良好清晰图像的图片框。谢谢

4

1 回答 1

0

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

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
于 2013-08-21T08:32:57.270 回答