c#绘制多个图像到picturebox中,不需要平铺
本文关键字:不需要 picturebox 绘制 图像 | 更新日期: 2023-09-27 18:15:42
我试图使用点绘制多个(20个小图像或更多)到单个图片框中,但是当我这样做时,它会复制图片框的每一行上的顶行。我已经到处搜索,并尝试了很多次,以得到这个正确的,但我不能弄清楚,谁可以帮助指出我在正确的方向?
我想完成的是:
- image1 image2 image3 image4 image1
- image2 image4 image1 image3 image3
- image4 image7 image2 image1 image6
- image3 image7 image3 image7 image6
但是我得到的是:
- image1 image2 image3 image4 image1
- image1 image2 image3 image4 image1
- image1 image2 image3 image4 image1
- image1 image2 image3 image4 image1
(当然没有数字)这里是我使用的代码
private void Form_Load(object sender, EventArgs e)
{
Image[] Files = new Image[7];
Bitmap a = new Bitmap(@"C:'icons'icon1.png");
Files[0] = new Bitmap(a, 80, 80);
image1 = Files[0];
Bitmap b = new Bitmap(@"C:'icons'icon2.png");
Files[1] = new Bitmap(b, 80, 80);
image2 = Files[1];
Bitmap c = new Bitmap(@"C:'icons'icon3.png");
Files[2] = new Bitmap(c, 80, 80);
image3 = Files[2];
Bitmap d = new Bitmap(@"C:'icons'icon4.png");
Files[3] = new Bitmap(d, 80, 80);
image4 = Files[3];
Bitmap h = new Bitmap(@"C:'icons'icon5.png");
Files[4] = new Bitmap(h, 80, 80);
image5 = Files[4];
Bitmap f = new Bitmap(@"C:'icons'icon6.png");
Files[5] = new Bitmap(f, 80, 80);
image6 = Files[5];
Bitmap g = new Bitmap(@"C:'icons'icon7.png");
Files[6] = new Bitmap(g, 80, 80);
image7 = Files[6];
pictureBox1.BackgroundImage = CombineBitmap(Files);
}
public static System.Drawing.Bitmap CombineBitmap(Image[] files)
{
List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
System.Drawing.Bitmap finalImage = null;
try
{
int width = 0;
int height = 0;
foreach (Image image in files)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;
images.Add(bitmap);
}
finalImage = new System.Drawing.Bitmap(width, height);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
{
g.Clear(System.Drawing.Color.Gray); // Change this to whatever you want the background color to be, you may set this to Color.Transparent as well
int offset = 15;
Point p1 = new Point(0, 0);
Point p2 = new Point(80, 0);
Point p3 = new Point(160, 0);
Point p4 = new Point(240, 0);
Point p5 = new Point(320, 0);
Point p6 = new Point(0, 80);
Point p7 = new Point(80, 80);
Point p8 = new Point(160, 80);
Point p9 = new Point(240, 80);
Point p10 = new Point(320, 80);
Point p11 = new Point(0, 160);
Point p12 = new Point(80, 160);
Point p13 = new Point(160, 160);
Point p14 = new Point(240, 160);
Point p15 = new Point(320, 160);
Point p16 = new Point(0, 240);
Point p17 = new Point(80, 240);
Point p18 = new Point(160, 240);
Point p19 = new Point(240, 240);
Point p20 = new Point(320, 240);
g.DrawImage(image1, p1);
g.DrawImage(image2, p2);
g.DrawImage(image3, p3);
g.DrawImage(image4, p4);
g.DrawImage(image1, p5);
g.DrawImage(image2, p6);
g.DrawImage(image4, p7);
g.DrawImage(image1, p8);
g.DrawImage(image3, p9);
g.DrawImage(image3, p10);
g.DrawImage(image4, p11);
g.DrawImage(image7, p12);
g.DrawImage(image2, p13);
g.DrawImage(image1, p14);
g.DrawImage(image6, p15);
g.DrawImage(image3, p16);
g.DrawImage(image7, p17);
g.DrawImage(image3, p18);
g.DrawImage(image7, p19);
g.DrawImage(image6, p20);
}
return finalImage;
}
catch (Exception ex)
{
if (finalImage != null)
finalImage.Dispose();
throw ex;
}
finally
{
foreach (System.Drawing.Bitmap image in images)
{
image.Dispose();
}
}
}
您的finalImage
位图的大小太小,并且只允许单行图像。然后,PictureBox控件平铺单行图像,以显示您正在看到的图像。
您正在绘制80像素宽80像素高的图像块。因此,位图应该足够宽以容纳80 * numColumns
像素,足够高以容纳80 * numRows
像素。
由于您有5列4行,因此使用如下:
finalImage = new System.Drawing.Bitmap(400, 320);