为什么图片框中的图像没有拉伸
本文关键字:图像 为什么 | 更新日期: 2023-09-27 17:57:45
我对C#中的图像处理相对陌生。我试图在图片框中缩放这个动态构建的BMP,但图像根本没有调整大小。我想要BMP填充整个图片框。图片框是555 x 555。它只是显示BMP的原始大小(100 x 100)。有什么想法吗?
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;
}
我也尝试过缩放大小模式。这没什么区别。
您可以利用.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);