本文實例講述了C#圖像處理之圖像平移的方法。分享給大家供大家參考。具體如下:
//定義圖像平移函數private static Bitmap offsetp(Bitmap a,int s,int v){ System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle (0,0,a.Width ,a.Height) ,System .Drawing .Imaging .ImageLockMode .ReadWrite ,a.PixelFormat ); IntPtr ptr = srcData.Scan0; int bytes = srcData.Stride * a.Height; byte[]grayVlaues=new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr ,grayVlaues ,0,bytes); byte[] tempArray=new byte[bytes]; for (int i = 0; i < bytes; i++) { tempArray[i] = 255; } for (int i = 0; i < a.Width * 3; i += 3) { if ((i + s*3) < a.Width*3 && (i + s*3) > 0) { for (int j = 0; j < a.Height; j++) { if ((j + v) < a.Height && (j + v) > 0) { tempArray[(i + s * 3) + (j + v) * srcData.Stride] = grayVlaues[i + j * srcData.Stride]; tempArray[i + s * 3 + 1 + (j + v) * srcData.Stride] = grayVlaues[i + 1 + j * srcData.Stride]; tempArray[i + s * 3 + 2 + (j + v) * srcData.Stride] = grayVlaues[i + 2 + j * srcData.Stride]; } } } } grayVlaues = (byte[])tempArray.Clone(); System.Runtime.InteropServices.Marshal.Copy(grayVlaues ,0,ptr, bytes); a.UnlockBits(srcData ); return a;}
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答