我正在尝试使用点将多个(20个小图像或更多图像)绘制到单个图片框中,但是当我这样做时,它会复制图片框每行的顶行。 我到处搜索并尝试了很多次才能做到这一点,但我无法弄明白,任何人都可以帮我指出正确的方向吗?
我想要完成的是:
(没有当然的数字) 这是我正在使用的代码
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();
}
}
}
答案 0 :(得分:0)
finalImage
位图的大小太小,只允许单行图像。然后,PictureBox控件将平铺单行图像,为您提供所见的图像。
您正在以80像素宽,80像素高的块绘制图像。因此,位图应足够宽以容纳80 * numColumns
像素,并且足够高以容纳80 * numRows
像素。
由于您有5列和4行,请使用:
finalImage = new System.Drawing.Bitmap(400, 320);