C#图片切割

图片切割就是把一幅大图片按用户要求切割成多幅小图片。dotnet环境下系统提供了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);
                }
            }
           
        }

     
    }

 

程序员只需要调用Cut函数就可以应用了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值