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:
if (BrowserHidden.HasFile)
{
strFileExtension = Path.GetExtension(BrowserHidden.FileName);
strFileName = "cylinder-" + gvCylinderData.DataKeys[gvCylinderData.SelectedIndex].Values["CylinderID"] + strFileExtension;
if ((strFileExtension == ".jpg") || (strFileExtension == ".jpeg") || (strFileExtension == ".gif") || (strFileExtension == ".png"))
{
if (!Directory.Exists(strFolderPath))
{
Directory.CreateDirectory(strFolderPath);
}
resizeImage(strFolderPath, strFileName);
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);
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;
}
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;
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);
}
}