我对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。这没有任何区别。
答案 0 :(得分: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);