翻腾手指

Code Art

博客园 首页 新随笔 联系 订阅 管理

自己做的图片分割的小系统

能把一张大图片按照自己指定的大小来分割成几个小图片,比如 100*100的一个图片可以把它分割成10张100*10的图片,能保存当前流行的格式。具有错误日志功能,后台线程处理图片切割
这里小弟附上源代码,供大家参考,做的很粗陋,大家有什么好的想法可以交流下

 

源码下载地址:ImageCutOrSplit

 

核心代码如下:

 

        /// <summary>
        /// 图片分割
        /// </summary>
        /// <param name="originalPath">原始图片路径</param>
        /// <param name="portionWidth">分割后图片宽</param>
        /// <param name="portionHeight">分割后图片高</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="fileName">文件名</param>
        /// <param name="format">图片格式</param>
        /// <param name="isSave">是否保存</param>
        /// <returns>返回值</returns>
        public static Image[] SplitImage(string originalPath, int portionWidth, int portionHeight, string savePath, string fileName, ImageFormat format, bool isSave)
        {
            Image[] images = null;
            if (!File.Exists(originalPath))
                return images;
            using (Stream imgStream = File.OpenRead(originalPath))
            {
                using (Bitmap bmp = new Bitmap(imgStream))
                {
                    images = Split(bmp, portionWidth, portionHeight, savePath, fileName, format, isSave);
                }
            }
            return (Image[])images.Clone();
        }
        /// <summary>
        /// 分割图片
        /// </summary>
        /// <param name="img">原始图片</param>
        /// <param name="portionWidth">分割后图片宽</param>
        /// <param name="portionHeight">分割后图片高</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="fileName">文件名</param>
        /// <param name="format">图片格式</param>
        /// <param name="isSave">是否保存</param>
        /// <returns>返回值</returns>
        private static Image[] Split(Image img, int portionWidth, int portionHeight, string savePath, string fileName, ImageFormat format, bool isSave)
        {
            int imgIndex = 0;
            //原始图片能分割成多少块新规格的图片
            int maxHeight = img.Height;
            int maxWidth = img.Width;
            int heightNumber = (maxHeight % portionHeight) > 0 ? (maxHeight / portionHeight) + 1 : (maxHeight / portionHeight);
            int widthNumber = (maxWidth % portionWidth) > 0 ? (maxWidth / portionWidth) + 1 : (maxWidth / portionWidth);
            int allNumber = heightNumber * widthNumber;

            Image[] images = new Image[allNumber];
            using (Bitmap imgSplitted = new Bitmap(portionWidth, portionHeight))
            {
                using (Graphics g = Graphics.FromImage(imgSplitted))
                {
                    for (int y = 0; y < maxHeight; y += portionHeight)
                    {
                        for (int x = 0; x < maxWidth; x += portionWidth)
                        {
                            try
                            {
                                g.Clear(Color.Transparent);
                                g.DrawImage(img, new Rectangle(0, 0, portionWidth, portionHeight), x, y, portionWidth, portionHeight, GraphicsUnit.Pixel);
                                g.Save();
                                if (isSave)
                                {
                                    if (!Directory.Exists(savePath))
                                    {
                                        Directory.CreateDirectory(savePath);
                                        Thread.Sleep(1000);
                                    }
                                    imgSplitted.Save(Path.Combine(savePath, fileName + imgIndex.ToString() + "." + format.ToString()), format);
                                }
                                images[imgIndex] = imgSplitted.Clone() as Image;
                                imgIndex++;
                            }
                            catch (Exception ex)
                            {
                                throw new LogException("Split", ex.Message);
                            }
                        }
                    }
                }
            }
            return images;
        }

posted on 2008-08-27 15:25  nerocool  阅读(4606)  评论(0编辑  收藏  举报