標籤:style blog color os io 檔案 for ar
映像的剪裁方法,從中間剪裁返回剪裁後的左右兩頁,方法實現如下:
1 /// <summary> 2 /// 圖片裁剪,返回左右兩頁 3 /// </summary> 4 /// <param name="Img">圖片</param> 5 /// <param name="RightMargin">從中間剪裁時左邊圖片向右位移量</param> 6 /// <param name="LeftMargin">從中間剪裁時右邊圖片向左位移量</param> 7 /// <returns>Dictionary</returns> 8 public Dictionary<string, Bitmap> ImgDiv(Image Img, int RightMargin, int LeftMargin) 9 {10 Dictionary<string, Bitmap> DictImg = new Dictionary<string, Bitmap>();11 //擷取圖片寬高12 int Width = Img.Width;13 int Height = Img.Height;14 //擷取圖片水平和垂直的解析度15 float dpiX = Img.HorizontalResolution;16 float dpiY = Img.VerticalResolution;17 //建立一個位元影像檔案18 Bitmap BitmapResult = new Bitmap((Width / 2) + RightMargin, Height, PixelFormat.Format24bppRgb);19 //設定位元影像檔案的水平和垂直解析度 與Img一致20 BitmapResult.SetResolution(dpiX, dpiY);21 //在位元影像檔案上填充一個矩形框22 Graphics Grp = Graphics.FromImage(BitmapResult);23 System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, (Width / 2) + RightMargin, Height);24 Grp.DrawImage(Img, 0, 0, Rec, GraphicsUnit.Pixel);25 //返回位元影像檔案26 Grp.Dispose();27 //擷取左邊映像28 DictImg["Left"] = BitmapResult;29 //在位元影像檔案上填充一個矩形框30 Bitmap BitmapRight = new Bitmap((Width / 2) + LeftMargin, Height, PixelFormat.Format24bppRgb);31 BitmapRight.SetResolution(dpiX, dpiY);32 Graphics GrpRight = Graphics.FromImage(BitmapRight);33 System.Drawing.Rectangle RecRight = new System.Drawing.Rectangle((Width / 2) - LeftMargin, 0, (Width / 2) + LeftMargin, Height);34 GrpRight.DrawImage(Img, 0, 0, RecRight, GraphicsUnit.Pixel);35 //擷取右邊映像36 DictImg["Right"] = BitmapRight;37 GrpRight.Dispose();38 GC.Collect();39 //返回位元影像檔案40 return DictImg;41 }