system.drawing.dll中发生了'system.io.filenotfoundexception'类型的异常,但未在用户代码中处理 [英] An exception of type 'system.io.filenotfoundexception' occurred in system.drawing.dll but was not handled in user code

查看:331
本文介绍了system.drawing.dll中发生了'system.io.filenotfoundexception'类型的异常,但未在用户代码中处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile( Path.GetFullPath(file.FileName));

这给了我错误我想要给我的电脑中的任何图像的路径,但我不能。任何解决方案??



我尝试过:



that gave me error i want to give path of any image which is loacted in my computer but i cant.any solution ??

What I have tried:

var file = Request.Files[0];
                        //HttpPostedFileBase hpf = Request.Files[0];

                        if (file != null && file.ContentLength > 0)
                        {
                            //var fileName = Path.GetFileName(file.FileName);                        
                            var fileExtension = Path.GetExtension(file.FileName);
                            var allowedExtensions = new[] { ".bmp", ".png", ".jpg", "jpeg", ".gif" };
                            if (allowedExtensions.Contains(fileExtension))
                            {
                                //Delete files
                                var pathD = Server.MapPath("~/Avatar/1");
                                var images = Directory.GetFiles(pathD, CustomerID + ".*");
                                for (int i = 0; i < images.Length; i++)
                                    System.IO.File.Delete(Server.MapPath(("~/Avatar/1/") + Path.GetFileName(images[i])));

                                //Up files
                                var fileName = CustomerID + fileExtension;
                                var path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName);
                                //var store = Path.GetFullPath(file.FileName);
                            
                                //System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
                                var test = Server.MapPath(file.FileName);
                                var test2 = Path.GetFullPath(file.FileName);
                                System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile( Path.GetFullPath(file.FileName));
                                int imageHeight = imageToBeResized.Height;
                                int imageWidth = imageToBeResized.Width;
                                int maxHeight = 400;
                                int maxWidth = 400;
                                imageHeight = (imageHeight * maxWidth) / imageWidth;
                                imageWidth = maxWidth;

                                if (imageHeight > maxHeight)
                                {
                                    imageWidth = (imageWidth * maxHeight) / imageHeight;
                                    imageHeight = maxHeight;
                                }

                                Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
                                System.IO.MemoryStream stream = new MemoryStream();
                                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                                stream.Position = 0;
                                byte[] imasave = new byte[stream.Length + 1];
                                stream.Read(imasave, 0, imasave.Length);

                                var newImage = System.Drawing.Image.FromStream(stream);
                                newImage.Save(path);

推荐答案

这是你的回答:



c# - 上传路径的全名与文件上传器 - 堆栈溢出 [ ^ ]
here is your answer :

c# - Full name of uploaded path with file uploader - Stack Overflow[^]


您不应该从客户端计算机打开图像。而是将副本保存在托管目录中并尝试访问它。



You are not supposed to open a Image from Client machine. Instead save a copy in your hosted Directory and try to access it.

string fileName = FileUpload1.FileName;
          var path = Path.Combine(Server.MapPath("~/Images/"), fileName); // I have a folder called 'Images' in my root directory, configure it for your need
          FileUpload1.SaveAs(path);
          System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(path);


这篇关于system.drawing.dll中发生了'system.io.filenotfoundexception'类型的异常,但未在用户代码中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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