C#图片切割
圖片切割就是把一幅大圖片按用戶要求切割成多幅小圖片。dotnet環(huán)境下系統(tǒng)提供了GDI+類庫,為圖像操作處理提供了方便的接口。
下面是圖像切割小程序:
public class ImageManager
??? {
??????? /// <summary>
??????? /// 圖像切割
??????? /// </summary>
??????? /// <param name="url">圖像文件名稱</param>
??????? /// <param name="width">切割后圖像寬度</param>
??????? /// <param name="height">切割后圖像高度</param>
??????? /// <param name="savePath">切割后圖像文件保存路徑</param>
??????? /// <param name="fileExt">切割后圖像文件擴展名</param>
??????? public static void Cut(string url, int width, int height,string savePath,string fileExt,string logofile)
??????? {
??????????? Bitmap bitmap = new Bitmap(url);
??????????? Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
??????????? Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / width);
??????????? for (decimal i = 0; i < MaxRow; i++)
??????????? {
??????????????? for (decimal j = 0; j < MaxColumn; j++)
??????????????? {
??????????????????? string filename = i.ToString() + "," + j.ToString() + "." + fileExt;
??????????????????? Bitmap bmp = new Bitmap(width, height);
??????????????????
??????????????????? for (int offsetX = 0; offsetX < width; offsetX++)
??????????????????? {
??????????????????????? for (int offsetY = 0; offsetY < height; offsetY++)
??????????????????????? {
??????????????????????????? if (((j * width + offsetX) < bitmap.Width) && ((i * height + offsetY) < bitmap.Height))
??????????????????????????? {
??????????????????????????????? bmp.SetPixel(offsetX, offsetY, bitmap.GetPixel((int)(j * width + offsetX), (int)(i * height + offsetY)));
??????????????????????????? }
??????????????????????? }
??????????????????? }???????????????????
??????????????????? Graphics g = Graphics.FromImage(bmp);
??????????????????? g.DrawString("哲慧科技", new Font("黑體", 20), new SolidBrush(Color.FromArgb(70, Color.WhiteSmoke)), 60, height/2);//加水印
??????????????????? ImageFormat format = ImageFormat.Png;??????????????????
??????????????????? switch (fileExt.ToLower())
??????????????????? {
??????????????????????? case "png":
??????????????????????????? format = ImageFormat.Png;
??????????????????????????? break;
??????????????????????? case "bmp":
??????????????????????????? format = ImageFormat.Bmp;
??????????????????????????? break;
??????????????????????? case "gif":
??????????????????????????? format = ImageFormat.Gif;
??????????????????????????? break;?????????????????????????
??????????????????? }
??????????????????? bmp.Save(savePath+"//" + filename,format);
??????????????? }
??????????? }
???????????
??????? }
?????
??? }
?
程序員只需要調(diào)用Cut函數(shù)就可以應(yīng)用了。
? 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
- 上一篇: hive(II)--sql考查的高频问题
- 下一篇: C# 修改打印机名称