如何从函数返回新图像

本文关键字:图像 新图像 返回 函数 | 更新日期: 2023-09-27 18:30:59

我添加了行

if (newWidth >= 1 && newHeight >= 1)

现在我收到错误

错误 1"Images_Batch_Resize.Form1.ResizeImage(string、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返回值(在最后一个)。将 return 语句添加到其他 if 或在此函数的末尾添加类似 return 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 ) 
        }

在您的代码中,如果代码没有命中最后一个if它将不会返回任何值,但是您减速此方法将在结束时返回Bitmap,因此编译希望在每种情况下对于该函数的用户来说,某种Bitmap都将返回(即使作为null