陋室铭
永远也不要停下学习的脚步(大道至简至易)

posts - 2169,comments - 570,views - 413万

public string ReturnHtml;
    // 不含扩展名的文件名
    private string _fileNameWithoutExtension;
    // 文件扩展名
    private string _fileExtension;
    // 文件所属的文件夹
    private string _fileDirectory;
    /// <summary>
    ///
    /// </summary>
    /// <param name="inputImgPath">文件虚拟路径</param>
    /// <param name="cropWidth">每块宽</param>
    /// <param name="cropHeight">每块高</param>
    /// <returns></returns>
    public string Cropping(string inputImgPath, int cropWidth, int cropHeight)
    {
        this._fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(inputImgPath);
        this._fileExtension = System.IO.Path.GetExtension(inputImgPath);
        this._fileDirectory = System.IO.Path.GetDirectoryName(inputImgPath);

        // 装载要分隔的图片
        System.Drawing.Image inputImg = System.Drawing.Image.FromFile(Server.MapPath(inputImgPath));
        int imgWidth = inputImg.Width;
        int imgHeight = inputImg.Height;

        // 计算要分几格
        int widthCount = (int)Math.Ceiling((imgWidth * 1.00) / (cropWidth * 1.00));
        int heightCount = (int)Math.Ceiling((imgHeight * 1.00) / (cropHeight * 1.00));
        //----------------------------------------------------------------------
        ArrayList areaList = new ArrayList();

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<table cellpadding='0' cellspacing='0' border='0'>");
        sb.Append(System.Environment.NewLine);

        int i = 0;
        for (int iHeight = 0; iHeight < heightCount; iHeight++)
        {
            sb.Append("<tr>");
            sb.Append(System.Environment.NewLine);
            for (int iWidth = 0; iWidth < widthCount; iWidth++)
            {
                //string fileName = "<img src='http://localhost/SRcommBeijingFile/" + this._fileNameWithoutExtension + " _" + i.ToString() + this._fileExtension + "'>";
                string fileName = string.Format("<img src='http://localhost/Example/Images/{0}_{1}{2}' />", this._fileNameWithoutExtension, i, this._fileExtension);
                sb.Append("<td>" + fileName + "</td>");
                sb.Append(System.Environment.NewLine);


                int pointX = iWidth * cropWidth;
                int pointY = iHeight * cropHeight;
                int areaWidth = ((pointX + cropWidth) > imgWidth) ? (imgWidth - pointX) : cropWidth;
                int areaHeight = ((pointY + cropHeight) > imgHeight) ? (imgHeight - pointY) : cropHeight;
                string s = string.Format("{0};{1};{2};{3}", pointX, pointY, areaWidth, areaHeight);

                Rectangle rect = new Rectangle(pointX, pointY, areaWidth, areaHeight);
                areaList.Add(rect);
                i++;
            }
            sb.Append("</tr>");
            sb.Append(System.Environment.NewLine);
        }

        sb.Append("</table>");


        //----------------------------------------------------------------------   

        for (int iLoop = 0; iLoop < areaList.Count; iLoop++)
        {
            Rectangle rect = (Rectangle)areaList[iLoop];
            string fileName = this._fileDirectory + "\\" + this._fileNameWithoutExtension + "_" + iLoop.ToString() + this._fileExtension;
            Bitmap newBmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            Graphics newBmpGraphics = Graphics.FromImage(newBmp);
            newBmpGraphics.DrawImage(inputImg, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
            newBmpGraphics.Save();
            switch (this._fileExtension.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                    newBmp.Save(Server.MapPath(fileName), ImageFormat.Jpeg);
                    break;
                case ".gif":
                    newBmp.Save(Server.MapPath(fileName), ImageFormat.Gif);
                    break;
            }

        }
        inputImg.Dispose();
        string html = sb.ToString();
        return html;
    }

posted on   宏宇  阅读(360)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2007年2月 >
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 1 2 3
4 5 6 7 8 9 10

点击右上角即可分享
微信分享提示