0

我对 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。那没有任何区别。

4

1 回答 1

2

您可以利用 .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);
于 2013-02-06T03:31:21.850 回答