从函数返回2个变量到main

编程入门 行业动态 更新时间:2024-10-28 00:19:00
本文介绍了从函数返回2个变量到main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好,我正在处理一个上传图片,正如您在我的代码中看到的那样,如果图片太大,我会调整图片的大小.我想从resizeImage方法中传回以下变量:maxwidth,maxheight和newBMP的文件大小.我知道我可以用完参数,但是我不确定如何实现此技术,有人可以帮我吗? 这是我的代码:

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); } }

推荐答案

而不是使用out参数(故意笨拙),而不是返回一个类(或struct),该类给出有关已采取措施的信息?这样可以将所有信息保持在一起 然后,如果没有更改,它可以返回一个实例,或者返回null而不是void.或更妙的是,将其中一个属性设置为bool,以指示图像是否已调整大小,并始终返回实例. 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.

您可以使用Ref关键字: 例如 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 :)

Quote:

公共无效的resizeImage(字符串FolderPath,字符串FileName)

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);

更多推荐

从函数返回2个变量到main

本文发布于:2023-11-11 11:11:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   函数   main

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!