Click here to Skip to main content
15,895,142 members

return 2 variables from function to main

pmcm asked:

Open original thread
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);
            }
        }
Tags: C# (C# 4.0), ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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