Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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.
Posted
Updated 28-Apr-16 20:14pm
v2
Comments
Karthik_Mahalingam 29-Apr-16 0:57am    
file.SaveAs(result); // result should be a path.
Rakib Ahmed 29-Apr-16 1:01am    
you did not get my question i think i just want to convert path so that it will be assignable to myfile
Karthik_Mahalingam 29-Apr-16 1:54am    
Please use Reply button.
Rakib Ahmed 29-Apr-16 1:02am    
path is a var type i want to convert it fileupload type
Sergey Alexandrovich Kryukov 29-Apr-16 1:27am    
And what makes you thinking that you somehow could type cast it? :-)
—SA

try this

C#
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);


You cannot assign path to the upload file control. its read only property (FileName)
 
Share this answer
 
Look at your code:
C#
var path = Path.Combine(Server.MapPath("~/Avatar/1/"), fileName);
...
FileUpload MyFile = new FileUpload();
MyFile = path;

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:
C#
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);
        }
    }
 
Share this answer
 
Comments
Rakib Ahmed 29-Apr-16 6:23am    
string CustomerID = "";
if (Session["RoleID"] != null)
{
CustomerID = Request.Form["CustomerID"];

//string CustomerID = Request.Form["Customer.CustomerID"];
if (CustomerID != null)
{
if (Request.Files.Count > 0)
{
var file = 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);




System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(file);
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);
Rakib Ahmed 29-Apr-16 6:24am    
i want to pass System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(file); that gave me error any idea how to resolve
OriginalGriff 29-Apr-16 6:39am    
What error?
Rakib Ahmed 2-May-16 0:13am    
System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(file);
this lines give me error cannot convert from 'System.Web.HttpPostedFileBase' to 'string'
OriginalGriff 2-May-16 3:05am    
Simple: it isn't a file!
It's an upload, which is a stream of bytes.

Image imageToBeResized = Image.FromStream(file.InputStream, true, true);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900