天天看點

Win8Metro(C#)數字圖像處理--2.18圖像平移變換



[函數名稱]

圖像平移變換函數TranslationProcess(WriteableBitmap

src,int x,int

y)

Win8Metro(C#)數字圖像處理--2.18圖像平移變換

[函數代碼]

       ///<summary>

       ///

Translation process.

       ///</summary>

       ///<param

name="src">Source image.</param>

name="x">Translate value of x.</param>

name="y">Translate value of y.</param>

       ///<returns></returns>

       publicstaticWriteableBitmap

TranslationProcess(WriteableBitmap src,int

x,int y)////18平移變換

       {

           if(src!=null

)

           {

           int

w = src.PixelWidth;

h = src.PixelHeight;

           WriteableBitmap

translateImage =newWriteableBitmap(w,

h);

           byte[]

temp = src.PixelBuffer.ToArray();

tempMask =newbyte[w

* h * 4];

           for

(int j = 0; j < h; j++)

               for

(int i = 0; i < w; i ++)

               {

                   if

(i + x < 0 || i + x >= w || j + y < 0 || j + y >= h)

                   {

                       tempMask[i * 4 + j * w * 4] = (byte)0;

                       tempMask[i * 4 + 1 + j * w * 4] = (byte)0;

                       tempMask[i * 4 + 2 + j * w * 4] = (byte)0;

                   }

                   else

                       tempMask[i * 4 + j * w * 4] = (byte)(temp[(i

+ x) * 4 + (j + y) * w * 4]);

                       tempMask[i * 4 + 1 + j * w * 4] = (byte)(temp[(i

+ x) * 4 + 1 + (j + y) * w * 4]);

                       tempMask[i * 4 + 2 + j * w * 4] = (byte)(temp[(i

+ x) * 4 + 2 + (j + y) * w * 4]);

                       tempMask[i * 4 + 3 + j * w * 4] = (byte)(temp[(i

+ x) * 4 + 3 + (j + y) * w * 4]);

               }

           }

           Stream

sTemp = translateImage.PixelBuffer.AsStream();

           sTemp.Seek(0,

SeekOrigin.Begin);

           sTemp.Write(tempMask, 0, w * 4 * h);

           return

translateImage;

           else

               returnnull;

           }  

       }

[圖像效果]

Win8Metro(C#)數字圖像處理--2.18圖像平移變換

繼續閱讀