2012-11-06 39 views
1

我想要的Bitmap秒的數組合併成一個單一Bitmap。給定一個Bitmap[,]陣列b這樣(假設這些圖片看起來像字符):合併位圖的數組到一個單一的位圖

b[0,0] = 1 
b[1,0] = 2 
b[0,1] = 3 
b[1,1] = 4 

我要生成

result = 12 
     34 

例如,給出以下四個Bitmap S:

b[0,0] =Image 0,0;

b[1,0] =Image 1,1;

b[0,1] =Image 0,1;

b[1,1] =Image 1,1;

我要生成 result =result;

這裏是我到目前爲止的代碼:

public static Bitmap Moisac(ref Bitmap[,] b) 
    { 
     BitmapData[,] bmData = new BitmapData[b.GetUpperBound(0) + 1, b.GetUpperBound(1) + 1]; 
     IntPtr[,] scan0 = new IntPtr[b.GetUpperBound(0) + 1, b.GetUpperBound(1) + 1]; 
     unsafe 
     { 
      byte*[,] p = new byte*[b.GetUpperBound(0) + 1,b.GetUpperBound(1) + 1]; 
      for (int i = 0; i <= b.GetUpperBound(0); i++) 
       for (int j = 0; j <= b.GetUpperBound(1); j++) 
        if (b[i, j].Width != b[0, 0].Width | b[i, j].Height != b[0, 0].Height) 
         throw new ArgumentException(
          "Width and Height properties of all elements of b must be equal.", 
          "b"); 

      int oneW = b[0, 0].Width; 
      int oneH = b[0, 0].Height; 
      int overallWidth = oneW * (b.GetUpperBound(0) + 1); 
      int overallHeight = oneH * (b.GetUpperBound(1) + 1); 
      Bitmap result = new Bitmap(b[0, 0], overallWidth, overallHeight); 

      for (int i = 0; i <= b.GetUpperBound(0); i++) 
       for (int j = 0; j <= b.GetUpperBound(1); j++) 
       { 
        bmData[i, j] = b[i, j].LockBits(new Rectangle(0, 0, oneW, oneH), 
                ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); 
        scan0[i, j] = bmData[i, j].Scan0; 
        p[i, j] = (byte*)(void*)scan0[i, j]; 
       } 

      BitmapData rbmData = result.LockBits(new Rectangle(0, 0, overallWidth, overallHeight), 
               ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 

      int stride = bmData[0, 0].Stride; 
      int nOffset = stride - 3*b[0, 0].Width; 

      int rStride = rbmData.Stride; 
      IntPtr rScan0 = rbmData.Scan0; 
      byte* rp = (byte*) (void*) rScan0; 

      for (int imgY = 0; imgY < b.GetUpperBound(1); ++imgY) 
      { 
       for (int imgX = 0; imgX <= b.GetUpperBound(0); ++imgX) 
       { 
        byte* currp = p[imgX, imgY]; 
        for (int y = 0; y < oneH; ++y) 
        { 
         for (int x = 0; x < 3*oneW; ++x) 
         { 
          rp[rStride*(imgY*oneH + y) + 3*imgX*oneW + x] = currp[0]; 
          ++currp; 
         } 
         currp += nOffset; 
        } 
       } 
      } 

      for (int i = 0; i <= b.GetUpperBound(0); i++) 
       for (int j = 0; j <= b.GetUpperBound(1); j++) 
        b[i, j].UnlockBits(bmData[i,j]); 


      result.UnlockBits(rbmData); 
      return result; 
     } 
    } 

看到的圖像在專輯here。他們都不會在這裏顯示。

+0

你需要合併他們這樣也可以吸引他們? –

+0

我需要合併數組中的任意圖像。但是,我修正了它。 –

回答

1

我做過的最愚蠢的錯誤。但是,如果它可以幫助別人,改變

for (int imgY = 0; imgY < b.GetUpperBound(1); ++imgY) 

for (int imgY = 0; imgY <= b.GetUpperBound(1); ++imgY) 

(在<應該<=)。

0

我做了一個基於你的代碼的版本,複製像素的行而不是像素。至少在我的方塊似乎工作(更快)。也許你喜歡它。每個循環內的最小變化。使用 林真的只爲串聯......扭曲的圖像轉換爲圓環多個攝像機圖像的360度簡單視圖之前...

public static Bitmap Mosaic(ref Bitmap[,] b) 
    { 
     BitmapData[,] bmData = new BitmapData[b.GetUpperBound(0) + 1, b.GetUpperBound(1) + 1]; 
     IntPtr[,] scan0 = new IntPtr[b.GetUpperBound(0) + 1, b.GetUpperBound(1) + 1]; 
     unsafe 
     { 
      byte*[,] p = new byte*[b.GetUpperBound(0) + 1, b.GetUpperBound(1) + 1]; 
      for (int i = 0; i <= b.GetUpperBound(0); i++) 
       for (int j = 0; j <= b.GetUpperBound(1); j++) 
        if (b[i, j].Width != b[0, 0].Width | b[i, j].Height != b[0, 0].Height) 
         throw new ArgumentException(
          "Width and Height properties of all elements of b must be equal.", 
          "b"); 

      int oneW = b[0, 0].Width; 
      int oneH = b[0, 0].Height; 
      int overallWidth = oneW * (b.GetUpperBound(0) + 1); 
      int overallHeight = oneH * (b.GetUpperBound(1) + 1); 
      Bitmap result = new Bitmap(b[0, 0], overallWidth, overallHeight); 

      for (int i = 0; i <= b.GetUpperBound(0); i++) 
       for (int j = 0; j <= b.GetUpperBound(1); j++) 
       { 
        bmData[i, j] = b[i, j].LockBits(new Rectangle(0, 0, oneW, oneH), 
                ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 
        scan0[i, j] = bmData[i, j].Scan0; 
        p[i, j] = (byte*)(void*)scan0[i, j]; 
       } 

      BitmapData rbmData = result.LockBits(new Rectangle(0, 0, overallWidth, overallHeight), 
               ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); 

      int stride = bmData[0, 0].Stride; 
      int nOffset = stride - 4 * b[0, 0].Width; 

      int rStride = rbmData.Stride; 
      IntPtr rScan0 = rbmData.Scan0; 
      byte* rp = (byte*)(void*)rScan0; 



      for (int imgY = 0; imgY <= b.GetUpperBound(1); ++imgY) 
      { 
        for (int y = 0; y < oneH; ++y) 
        { 
         byte* currp = p[0, imgY]; 
         for (int imgX = 0; imgX <= b.GetUpperBound(0); ++imgX) 
         { 
          currp = p[imgX, imgY]; 
          currp += stride*y; 
         byte[] buffer = new byte[stride]; 
         Marshal.Copy(new IntPtr(currp), buffer, 0, buffer.Length); 
         Marshal.Copy(buffer, 0, new IntPtr(rp), buffer.Length); 

         rp += stride; 

         } 



        } 



      } 

      for (int i = 0; i <= b.GetUpperBound(0); i++) 
       for (int j = 0; j <= b.GetUpperBound(1); j++) 
        b[i, j].UnlockBits(bmData[i, j]); 


      result.UnlockBits(rbmData); 
      return result; 
     } 
    } 
相關問題