如何在提交时压缩图像 [英] how to compress image on submitting

查看:81
本文介绍了如何在提交时压缩图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上在我的表单上单击提交按钮源名称,sourceurl和图像名称正在保存但我的要求是我想在savind之前压缩图像所以首先我保存图像与original_ +图像名称然后我压缩该图像和存储该图像的真实姓名...最后我删除解压缩图像...我的问题是我的程序第一次运行完美但第二次运行直到图像删除然后跳出按钮单击功能没有数据库中的savind数据.......任何人都可以告诉我我做错了什么...

提前谢谢你...

我的代码是:







actually in my form on click of submit button source name,sourceurl and image name are saving but my requirement is that i want to compress image before savind so first i am saving image with original_+image name then i compress that image and store that image with its real name...in last i am deleting uncompress image...my problem is that my program run perfectly on first time but second run till image deletion and then jump out of button click function without savind data in database.......anybody can tell what i am doing wrong...
thank you in advance...
my code is:



SourceImage1.SaveAs(HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/original_" + SourceImage1.FileName));

                    Bitmap OriginalBM = new Bitmap(HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/original_" + SourceImage1.FileName));


                        Size newSize = new Size(300, 300);
                        Bitmap ResizedBM = new Bitmap(OriginalBM, newSize);
                        string ext = Path.GetExtension(HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/original_" + SourceImage1.FileName));
                        if (ext.ToLower() == ".gif" )
                        {
                            string dest = HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/" + SourceImage1.FileName);

                            ResizedBM.Save(dest, ImageFormat.Gif);
                        }
                        else if (ext.ToLower() == ".png")
                        {
                            string dest = HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/" + SourceImage1.FileName);

                            ResizedBM.Save(dest, ImageFormat.Png);

                        }
                            else if (ext.ToLower() == ".tiff")
                        {
                            string dest = HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/" + SourceImage1.FileName);

                            ResizedBM.Save(dest, ImageFormat.Tiff);

                        }
                            else if (ext.ToLower() == ".bmp")
                        {
                            string dest = HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/" + SourceImage1.FileName);

                            ResizedBM.Save(dest, ImageFormat.Bmp);

                        }
                            
                        else
                            {
                            string dest = HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/" + SourceImage1.FileName);

                            ResizedBM.Save(dest, ImageFormat.Jpeg);
                        }
                        OriginalBM = null;
                        ResizedBM = null;
                        GC.Collect();
                        File.Delete(HttpContext.Current.Server.MapPath(@"~/Upload_Files/source/original_" + SourceImage1.FileName));
    
                        
                        string queryInsert = "";

                        string sourceurl = txturl.Text;
                   

                        string sourrcename = txtSource.Text;
                                 queryInsert = "insert into source(SourceName,SourceUrl,SourceImage) values('" + sourrcename.Trim() + "','" + sourceurl.Trim() + "','" + SourceImage1.FileName.Trim() + "')";

                        
                        SqlCommand cmdInsert = new SqlCommand(queryInsert, con);
                        cmdInsert.ExecuteNonQuery();

推荐答案

当您第二次将图片提交给服务器时,已经存在具有该名称的文件,因此保存操作失败并出现异常。
When you submit the picture the second time to the server, there already exists a file with that name, and consequently the Save operation fails with an exception.


这篇关于如何在提交时压缩图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆