Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

I' m trying to create a control for my web site where the user will be able to upload images, due to a javascript code i need the images with specific names and specific size "width/heigth",
So in my C# file I create the following code in order to make it but it didn't work,
the rename code t works fine , the issue is the resize of the image which it didn't work (I found a relative article in codeproject with resize images but unfortunatelly it uses Mvc tech and I don't).
Could anyone help me what I should modify in my code in order to fix the resizing of the image which will be upload it?

Thnx in advance!!!

C#
string subimages2 = "~/gallery/thumbs/";
        Boolean fileOK2 = false;
        String path2 = Server.MapPath(subimages2);
        if (FileUpload1.HasFile)
        {
            String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK2 = true;
                }
            }
        }

        if (fileOK2)
        {
            try
            {
                   int count = 10;
               
                string fileNameWithoutExtension =    Path.GetFileNameWithoutExtension(FileUpload1.FileName);
                string fileExtension = Path.GetExtension(FileUpload1.FileName);
                fileNameWithoutExtension = Convert.ToString(count);
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/gallery/thumbs/" + fileNameWithoutExtension + fileExtension));

                //modify width heigth
                HttpPostedFile pf = FileUpload1.PostedFile;
                System.Drawing.Image bm = System.Drawing.Image.FromStream(pf.InputStream);
                bm = ResizeBitmap((Bitmap)bm, 150, 150); /// new width, height
                                
                Label1.Text = "File uploaded!";

            }
            catch (Exception ex)
            {
                Label1.Text = "File could not be uploaded.";
            }
        }
        else
        {
            Label1.Text = "Cannot accept files of this type.";
        }
    }
    private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
    {
        Bitmap result = new Bitmap(nWidth, nHeight);
        using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
            g.DrawImage(b, 0, 0, nWidth, nHeight);
        return result;
    }
Posted
Updated 11-Apr-14 5:18am
v2

 
Share this answer
 
Comments
JasonTsoum77 11-Apr-14 11:27am    
Hi Nilesh thnx for your response the second solution I find it too but it didn't worked,
http://stackoverflow.com/questions/254419/asp-net-image-uploading-with-resizing[^]
I have tried many codes from the internet which suppose that they resize an image but nothing,
I will try and the first one that you send and I will inform tou thnx again
JasonTsoum77 11-Apr-14 11:41am    
It worked!!! the first solution "http://imageresizing.net/docs/howto/upload-and-resize[^]"
I modify my code just like that and of I have to install imageresizer (very good util I did't knew it)
Thnx for your help!!!

try
{
int count = 10;
// FileUpload1.PostedFile.SaveAs(path2 + FileUpload1.FileName);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string fileExtension = Path.GetExtension(FileUpload1.FileName);
fileNameWithoutExtension = Convert.ToString(count);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/gallery/thumbs/" + fileNameWithoutExtension + fileExtension));

foreach (string fileKey in HttpContext.Current.Request.Files.Keys)
{
HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];
if (file.ContentLength <= 0) continue;
ImageResizer.ImageJob i = new ImageResizer.ImageJob(file, "~/uploads/<guid>.<ext>", new ImageResizer.ResizeSettings("width=20;height=22;format=jpg;mode=max"));
i.CreateParentDirectory = true;
i.Build();
}

Label1.Text = "File uploaded!";

}
C#
public static Bitmap GetThumbnail(Bitmap source, int maxWidth, int maxHeight)
        {
            int iHeight = 0;
            int iWidth = 0;
            iHeight = source.Height;
            iWidth = source.Width;

            if (source.Width > maxWidth)
            {
                if (source.Width > source.Height)
                {
                    iWidth = maxWidth;
                    iHeight = source.Height * maxWidth / source.Width;
                }
                else
                {
                    iHeight = maxHeight;
                    iWidth = source.Width * maxHeight / source.Height;
                }
            }
            else if (source.Height > maxHeight)
            {
                if (source.Width > source.Height)
                {
                    iWidth = maxWidth;
                    iHeight = source.Height * maxWidth / source.Width;
                }
                else
                {
                    iHeight = maxHeight;
                    iWidth = source.Width * maxHeight / source.Height;
                }
            }
            Bitmap dest = new Bitmap(source, iWidth, iHeight);
            return dest;
        }
 
Share this answer
 
Comments
JasonTsoum77 12-Apr-14 11:14am    
Thnx for your help, it also work!!!

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