【问题标题】:Why is the image not being stretched in the picture box?为什么图片在图片框中没有被拉伸?
【发布时间】:2013-01-21 04:04:55
【问题描述】:

我对 C# 中的图像处理比较陌生。我正在尝试在图片框中缩放这个动态构建的 BMP,但图像根本没有调整大小。我希望 BMP 填充整个图片框。图片框为 555 x 555。它只是以原始大小 (100 x 100) 显示 BMP。有什么想法吗?

private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics gPB = e.Graphics;

    Bitmap bmp = new Bitmap(100, 100);
    Graphics gBmp = Graphics.FromImage(bmp);


    Brush whiteBrush = new SolidBrush(Color.White);
    gBmp.FillRectangle(whiteBrush, 0, 0, bmp.Width, bmp.Height);

    Brush redBrush = new SolidBrush(Color.IndianRed);
    gBmp.FillRectangle(redBrush, 0, 0, 20f, 20f);

    Brush greenBrush = new SolidBrush(Color.MediumSeaGreen);
    gBmp.FillRectangle(greenBrush, 20, 0, 20f, 20f);

    Brush blueBrush = new SolidBrush(Color.MediumSlateBlue);
    gBmp.FillRectangle(blueBrush, 0, 20, 20f, 20f);

    Brush yellowBrush = new SolidBrush(Color.LightYellow);
    gBmp.FillRectangle(yellowBrush, 20, 20, 20f, 20f);

    gPB.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);

    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

我也尝试过 Zoom SizeMode。这没有任何区别。

【问题讨论】:

    标签: c# image


    【解决方案1】:

    您可以利用 .NET 的 Interpolation 属性。看这里: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode.aspx

    gPB.InterpolationMode = InterpolationMode.NearestNeighbor;
    gPB.DrawImage(bmp, 0, 0, 555, 555);
    

    【讨论】:

    • 有趣。我从来没有想过在绘制图像时使用pictureBox的宽度和高度。插值模式有助于调整大小后的分辨率。这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    相关资源
    最近更新 更多