C# 如何从函数返回新图像?
我加了一行C# 如何从函数返回新图像?,c#,.net,winforms,C#,.net,Winforms,我加了一行 if (newWidth >= 1 && newHeight >= 1) 现在我得到了错误 错误1“Images\u Batch\u Resize.Form1.ResizeImagestring,int,int”:并非所有代码路径都返回值 private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight) { using (
if (newWidth >= 1 && newHeight >= 1)
现在我得到了错误
错误1“Images\u Batch\u Resize.Form1.ResizeImagestring,int,int”:并非所有代码路径都返回值
private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
{
using (Image originalImage = Image.FromFile(filename))
{
//Caluate new Size
int newWidth = originalImage.Width;
int newHeight = originalImage.Height;
double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
if (aspectRatio <= 1 && originalImage.Width > maxWidth)
{
newWidth = maxWidth;
newHeight = (int)Math.Round(newWidth / aspectRatio);
}
else if (aspectRatio > 1 && originalImage.Height > maxHeight)
{
newHeight = maxHeight;
newWidth = (int)Math.Round(newHeight * aspectRatio);
}
if (newWidth >= 1 && newHeight >= 1)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
//--Quality Settings Adjust to fit your application
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
}
}
您之所以会收到该错误,是因为函数并不总是返回值。如果newWidth不是>=1或newHeight不是>=1,则它将不会返回任何内容。如果它不满足这些要求,您将希望返回null或类似的值,这样您就知道有问题了 您只返回其中一个if in last中的值。将return语句添加到other if或在该函数的末尾添加类似returnnull的内容
在您的代码中,如果代码没有最后命中,如果它不会返回任何值,但是你要减速,这个方法会在最后返回位图,所以编译希望在任何情况下,对于该函数的用户,某种类型的位图都会返回,即使是空的如果newWidth或newHeight为零,你想返回什么?因为…当测试失败时会发生什么?它经过了那个代码块,然后呢?您需要在最后返回…返回null;可能
private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
{
using (Image originalImage = Image.FromFile(filename))
{
//Caluate new Size
int newWidth = originalImage.Width;
int newHeight = originalImage.Height;
double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
if (aspectRatio <= 1 && originalImage.Width > maxWidth)
{
newWidth = maxWidth;
newHeight = (int)Math.Round(newWidth / aspectRatio);
}
else if (aspectRatio > 1 && originalImage.Height > maxHeight)
{
newHeight = maxHeight;
newWidth = (int)Math.Round(newHeight * aspectRatio);
}
if (newWidth >= 1 && newHeight >= 1)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
//--Quality Settings Adjust to fit your application
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
}
return null ( or something )
}