无法将类型字符串隐式转换为system.web.ui.webcontrols.fileupload [英] Cannot implicitly convert type string to system.web.ui.webcontrols.fileupload

查看:80
本文介绍了无法将类型字符串隐式转换为system.web.ui.webcontrols.fileupload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串转换为system.web.ui.webcontrol.fileupload

MyFile = path;给了我错误,所以我想将路径转换为system.web.ui.webcontrol

因为路径是var任何想法?转换



i want to convert string to system.web.ui.webcontrol.fileupload
MyFile = path; gave me error so i want to convert path to system.web.ui.webcontrol
since path is var any idea?to convert

var fileName = CustomerID + fileExtension;
var path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName);                               
                               
//file.SaveAs(path);

//Session["Avatar"] = fileName;

FileUpload MyFile = new FileUpload();
MyFile = path;
            
System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(MyFile.PostedFile.InputStream);
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);
            
string result = System.Text.Encoding.UTF8.GetString(imasave);

file.SaveAs(result);





我尝试过:



以上代码。



What I have tried:

The above code.

推荐答案

试试这个



try this

var path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName); 

            //FileUpload MyFile = new FileUpload();
            //MyFile = path;


            System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(path);
            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);





您无法为上传文件控件分配路径。其只读属性(FileName)



You cannot assign path to the upload file control. its read only property (FileName)


查看您的代码:

Look at your code:
var path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName);
...
FileUpload MyFile = new FileUpload();
MyFile = path;



path 是一个字符串:它是一个正确的文件路径您的服务器。

您无法将其分配给持有FileUpload控件的变量 - 即使您可能无法工作,因为当您的用户指示其浏览器时,UploadControl仅加载数据将文件从他的系统上传到你的。

我认为你要做的是从你的文件夹加载一个图像,调整它的大小并保存调整后的图像。

如果所以,你不需要这一切!

试试这个:


path is a string: it's a "proper" path to file on your server.
You can't assign that to a variable holding a FileUpload Control - and even if you could it wouldn't work because the UploadControl is only loaded with data when your user instructs his browser to upload a file from his system to yours.
What I think you are trying to do is load an image from your folder, resize it and save the resized image.
If so, you don't need all that!
Try this:

string fileName = CustomerID + fileExtension;
string path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName);
using (Image orig = Image.FromFile(path))
    {
    int imageHeight = orig.Height;
    int imageWidth = orig.Width;
    if (imageHeight > maxHeight)
        {
        imageWidth = (imageWidth * maxHeight) / imageHeight;
        imageHeight = maxHeight;
        }
    using (Bitmap resized = new Bitmap(orig, imageWidth, imageHeight))
        {
        resized.Save(result, ImageFormat.Jpeg);
        }
    }


这篇关于无法将类型字符串隐式转换为system.web.ui.webcontrols.fileupload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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