2013-05-28 42 views
0

我试图用点绘制多个(20个小图像或更多)到单个图片框,但是当我这样做时,它复制了图片框每行上的首行。 我到处搜索,并尝试了很多次,以得到这个权利,但我不能弄明白,任何人都可以帮助指向正确的方向吗?C#将多个图像绘制成没有平铺的图片框

我想要做到的是:

  1. 图像1图像2图像3图像4图像1
  2. 图像2图像4图像1图像3图像3
  3. 图像4 image7图像2图像1 image6
  4. 图像3 image7图像3 image7 image6

但是我得到的是:

  1. 图像1图像2图像3图像4图像1
  2. 图像1图像2图像3图像4图像1
  3. 图像1图像2图像3图像4图像1
  4. 图像1图像2图像3图像4图像1

(当然没有数) 这里是我正在使用的代码

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

The siz您的finalImage位图的太小,只允许一行图像。然后,PictureBox控件将平铺单行图像,为您提供所看到的图像。

您正在以80像素宽80像素高的方块绘制图像。因此,位图应足够宽以容纳80 * numColumns像素,并且足够高以容纳80 * numRows像素。

既然你有5列4行,使用:

finalImage = new System.Drawing.Bitmap(400, 320); 
+0

哇哦不敢相信我漏掉了这一点。谢谢,这很快! – user2296611

+0

有时我们无法看到树林的森林:) – Corey