博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
System.Drawing.Image.Save(Savepath),保存为jpg格式,参数错误,文件0kb解决办法
阅读量:6533 次
发布时间:2019-06-24

本文共 2475 字,大约阅读时间需要 8 分钟。

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

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

 

 

解决代码:

             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);
            }

转载于:https://www.cnblogs.com/leqoqo/p/3499505.html

你可能感兴趣的文章
react-native 常见操作 及 git 补充
查看>>
MongoDB 自己定义函数
查看>>
Summary Day30
查看>>
逆向输出回环数组
查看>>
自己动手,实现“你的名字”滤镜
查看>>
高清摄像头MIPI CSI2接口浅解【转】
查看>>
C# CancellationTokenSource和CancellationToken的实现
查看>>
.Net IOC框架入门之一 Unity
查看>>
PCIE BAR空间
查看>>
winform命名规范
查看>>
如何用数学课件制作工具画角平分线
查看>>
Linux chmod命令及权限含义
查看>>
jrtplib编译指南
查看>>
VS2015 中统计整个项目的代码行数
查看>>
Anaconda入门使用指南
查看>>
UWP控件与DataBind
查看>>
bash: php: command not found
查看>>
XVIII Open Cup named after E.V. Pankratiev. Eastern Grand Prix
查看>>
数据恢复软件如何换机使用?
查看>>
《高性能mysql》到手
查看>>