Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
Hi everyone, I'm working with an upload image and as you can see with my code I'm resizing the image if it's too big. I'd like to pass back from the resizeImage method the following variables: maxwidth, maxheight and the filesize of the newBMP. I know I can use out parameters but I'm not sure how to implement this technique, can someone help me please?
Here's my code:
//Save Cylinder Image File;
            if (BrowserHidden.HasFile)
            {                
                strFileExtension = Path.GetExtension(BrowserHidden.FileName);
                strFileName = "cylinder-" + gvCylinderData.DataKeys[gvCylinderData.SelectedIndex].Values["CylinderID"] + strFileExtension;
                //Is the file type valid?
                if ((strFileExtension == ".jpg") || (strFileExtension == ".jpeg") || (strFileExtension == ".gif") || (strFileExtension == ".png"))
                {
                    if (!Directory.Exists(strFolderPath))
                    {
                        //create the new folder if it doesn't already exist
                        Directory.CreateDirectory(strFolderPath);
                    }
 
                    resizeImage(strFolderPath, strFileName); //i'd like to return the variable from this method

                    imgCurrentLogo.ImageUrl = "~/Admin/Images" + "/" + strFileName;
                }
                else
                {
                    lblUploadImage.Visible = true;
                    lblUploadImage.Text = "You must pick a jpg, jpeg, gif or png file";
                    txtLogo.Text = "";
                    txtPDFDatasheetPath.Text = "";
                    return;
                }
 
                Bitmap origBMP = new Bitmap(BrowserHidden.FileContent);
                string imgheight = "100";
                string imgwidth = "100";
                int fileSize = BrowserHidden.PostedFile.ContentLength;
 
                int maxwidth = int.Parse(imgwidth);
                int maxheight = int.Parse(imgheight);
                //Is the file too big to upload?
                if ((origBMP.Width > maxwidth) || (origBMP.Height > maxheight))
                {
                    lblUploadImage.Visible = true;
                    lblUploadImage.Text = "Image height x width is invalid: 100px(h) x 100px(w)";
                    txtLogo.Text = "";
                    txtPDFDatasheetPath.Text = "";
                    return;
                }
                //Is the file > 200KB?
                else if (fileSize > 204800)
                {
                    lblUploadImage.Visible = true;
                    lblUploadImage.Text = "File size exceeds maximum limit 200KB.";
                    txtLogo.Text = "";
                    txtPDFDatasheetPath.Text = "";
                    return;
                }
            }
 
public void resizeImage(string FolderPath, string FileName)
        {
            string fName, trgDir;
            trgDir = FolderPath;
            fName = FileName;
 
            Bitmap origBMP = new Bitmap(BrowserHidden.FileContent);
            string tempwidth = "100";
            string tempheight = "100";
            int maxwidth = int.Parse(tempwidth);
            int maxheight = int.Parse(tempheight);
            if (origBMP.Width <= maxwidth)
            {
                origBMP.Save(trgDir + fName);
                return;
            }
            else
            {
                int origWidth = origBMP.Width;
                int origHeight = origBMP.Height;
                int newWidth = maxwidth;
                int newHeight = maxheight; //newWidth * origHeight / origWidth;

                Bitmap newBMP = new Bitmap(origBMP, newWidth, newHeight);
                Graphics objGra = Graphics.FromImage(newBMP);               
                objGra.DrawImage(origBMP, new Rectangle(0, 0, newBMP.Width, newBMP.Height), 0, 0, origWidth, origHeight, GraphicsUnit.Pixel);
                objGra.Dispose();
                newBMP.Save(trgDir + fName);
            }
        }
Posted 16-Oct-12 22:20pm
pmcm850
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Rather than using out parameters (which are deliberately clumsy), why not return a class (or struct) which gives info about the actions taken? This keeps all the info together
Then instead of a void it could return an instance or null if no change has taken place. Or better, make one of the properties a bool which indicates if the image has been re-sized, and always return an instance.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use Ref keyword :
 
e.g.
class RefExample
    {
        static void Method(ref int i)
        {
            // Rest the mouse pointer over i to verify that it is an int. 
            // The following statement would cause a compiler error if i 
            // were boxed as an object.
            i = i + 44;
        }
 
        static void Main()
        {
            int val = 1;
            Method(ref val);
            Console.WriteLine(val);
 
            // Output: 45
        }
    }
 
Or out Keyword:
 
class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}
 
If you have multiple values of same type & you want return it from method, you can use array as a return type for that method Smile | :)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Quote:
public void resizeImage(string FolderPath, string FileName)

 
Change to
 
public void resizeImage(string FolderPath, string FileName, out int maxwidth, out int maxheight, out long filesize)
 

and then call it this way:
 
//...
int maxwidth;
int maxheight;
long filesize;
 
resizeImage(strFolderPath, strFileName, out maxwidth, out maxheight, out filesize);
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 352
1 OriginalGriff 344
2 Abdul Samad KP 145
3 CPallini 140
4 Richard MacCutchan 110
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,680
2 CPallini 4,850
3 George Jonsson 3,454
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100