Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
//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

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.
 
Share this answer
 
You can use Ref keyword :

e.g.
C#
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:

C#
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 :)
 
Share this answer
 
v2
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:

C#
//...
int maxwidth;
int maxheight;
long filesize;

resizeImage(strFolderPath, strFileName, out maxwidth, out maxheight, out filesize);
 
Share this answer
 

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