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

时间:2021-07-28 11:58:17

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

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

 

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

 

解决代码:

             try
            {
                using (System.Drawing.Image image = System.Drawing.Image.FromFile(@"D:\WWW\test\DownLoad\企业毕业证书.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" + "\r\n");
                    //新建一个画板
                    System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(image);
                    Response.Write("2" + "\r\n");
                    graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                    Response.Write("3" + "\r\n");

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

                    //设置字体颜色
                    System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                    Response.Write("5" + "\r\n");
                    graphic.DrawString("2", f, b, 729, 326);
                    Response.Write("6" + "\r\n");
                    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" + "\r\n");
                    //设置字体
                    System.Drawing.Font f1 = new System.Drawing.Font("宋体", 18);
                    Response.Write("8" + "\r\n");
                    graphic.DrawString("2", f1, b, 1412, 988);
                    graphic.DrawString("test", f1, b, 1471, 1132);
                    Response.Write("9" + "\r\n");
                    graphic.Dispose();
                    Response.Write("10" + "\r\n");

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

                    System.Drawing.Image outimg = image;

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

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

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

                    Response.Write(Savepath);

                }
            }
            catch (Exception ex)
            {

                Response.Write(ex.Message);
            }