• System.Drawing.Image.Save(Savepath),保存为jpg格式,参数错误,文件0kb解决办法


    问题场景:asp.net给图片添加文字水印保存为jpg格式时出现标题所描述错误(图片为.jpg格式);

    简单验证:用本机的画图程序打开,然后保存为jpg格式会出现警告框“画图程序不能存储该文件,保存被中断  所以文件未被保存“

    11

    解决代码:

                 try
                {
                    using (System.Drawing.Image image = System.Drawing.Image.FromFile(@"D:WWW estDownLoad企业毕业证书.jpg"))
                    {

                        #region 解决方案一,移除属性,可以保存为jpeg格式图片
                        System.Drawing.Imaging.PropertyItem[] pi = image.PropertyItems;
                        foreach (System.Drawing.Imaging.PropertyItem p in pi)
                        {
                            image.RemovePropertyItem(p.Id);
                        }

                        #endregion

                        Response.Write("1" + " ");
                        //新建一个画板
                        System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(image);
                        Response.Write("2" + " ");
                        graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                        Response.Write("3" + " ");


                        //设置字体
                        System.Drawing.Font f = new System.Drawing.Font("宋体", 12);
                        Response.Write("4" + " ");

                        //设置字体颜色
                        System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                        Response.Write("5" + " ");
                        graphic.DrawString("2", f, b, 729, 326);
                        Response.Write("6" + " ");
                        graphic.DrawString("2013", f, b, 1083, 1325);
                        graphic.DrawString("12", f, b, 1375, 1325);
                        graphic.DrawString("2", f, b, 1842, 1325);
                        graphic.DrawString("99", f, b, 2676, 1325);
                        graphic.DrawString("13", f, b, 656, 326);
                        graphic.DrawString("12", f, b, 1083, 1485);
                        graphic.DrawString("12", f, b, 1375, 1485);
                        graphic.DrawString("1", f, b, 1928, 1485);
                        graphic.DrawString("1", f, b, 2735, 1485);
                        graphic.DrawString("2013", f, b, 2494, 1837);
                        graphic.DrawString("12", f, b, 2673, 1837);
                        graphic.DrawString("31", f, b, 2790, 1837);
                        Response.Write("7" + " ");
                        //设置字体
                        System.Drawing.Font f1 = new System.Drawing.Font("宋体", 18);
                        Response.Write("8" + " ");
                        graphic.DrawString("2", f1, b, 1412, 988);
                        graphic.DrawString("test", f1, b, 1471, 1132);
                        Response.Write("9" + " ");
                        graphic.Dispose();
                        Response.Write("10" + " ");

                        string OutPath = Server.MapPath("UploadImg/毕业证书");
                        if (!Directory.Exists(OutPath))
                        {
                            Response.Write("11" + " ");
                            Directory.CreateDirectory(OutPath);
                        }
                        Response.Write("12" + " ");
                        string Savepath = Path.Combine(@"D:WWW estUploadImg毕业证书", "1.JPG");

                        System.Drawing.Image outimg = image;

                        Response.Write("13" + " ");
                        outimg.Save(Savepath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        #region 解决方案二,保存为png格式图片

                        outimg.Save(Savepath,System.Drawing.Imaging.ImageFormat.Png);

                        #endregion
                        outimg.Dispose();
                        Response.Write("14" + " ");
                        Response.Write("15" + " ");

                        Response.Write(Savepath);

                    }
                }
                catch (Exception ex)
                {

                    Response.Write(ex.Message);
                }

  • 相关阅读:
    通过代码获取log4net的文件路径
    关键字后面不加空格
    ASP.NET MVC 4 Content Map
    ASP.NET Overview
    ASP.NET 4 and Visual Studio 2010
    Newtonsoft.Json
    ASP.NET MVC
    log4net
    AutoMapper introduction
    ajax jsonp跨域
  • 原文地址:https://www.cnblogs.com/leqoqo/p/3499505.html
Copyright © 2020-2023  润新知