我可以在下面的代码中进一步改进吗?

时间:2011-07-30 21:15:22

标签: c#-4.0 image-processing image-manipulation

这是我用C#

生成方形缩略图的代码

我以前没有编码过这种类型的东西。 由于此代码将被大量使用,因此我在将其投入生产之前与您核实。

你看到有什么不好或可以进一步改善吗?

基本上我在一些文件夹中有一组缩略图。 那些缩略图不是正方形,我需要将它们全部变成正方形。 这将在文件夹中的每个文件的循环中调用。

这就是我所说的:

SaveSquareThumb(75, "C:\storage\images\temp\thumbs", "C:\storage\images\thumbs");

public static void SaveSquareThumb(int squareSize, string sourcePath, string savePath)
        {
            using (Bitmap srcImage = new Bitmap(sourcePath))
            {
                int width = srcImage.Width;
                int height = srcImage.Height;

                int x = 0;
                int y = 0;

                //Determine dimensions of resized version of the image 
                if (width > height)
                {
                    width = (int)Decimal.Round(Convert.ToDecimal((decimal)squareSize * ((decimal)width / (decimal)height)), 0);

                    height = squareSize;
                    // moves cursor so that crop is more centered 
                    x = Convert.ToInt32(Math.Ceiling(((decimal)(width - height) / 2M)));
                }
                else if (height > width)
                {
                    height = (int)Decimal.Round(Convert.ToDecimal((decimal)squareSize * ((decimal)height / (decimal)width)), 0);
                    width = squareSize;
                    // moves cursor so that crop is more centered 
                    y = Convert.ToInt32(Math.Ceiling(((decimal)(height - width) / 2M)));
                }
                else
                {
                    width = squareSize;
                    height = squareSize;
                }

                // required in case thumbnail creation fails?
                Image.GetThumbnailImageAbort dummyCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = qualityParam;

                //get thumbnail from source image
                using (Image thumb = srcImage.GetThumbnailImage(width, height, null, System.IntPtr.Zero))
                {
                    //Create a Crop Frame to apply to the Resized Image 
                    using (Bitmap myBitmapCropped = new Bitmap(squareSize, squareSize))
                    {
                        using (Graphics myGraphic = Graphics.FromImage(myBitmapCropped))
                        {
                            //Apply the Crop to the Resized Image 
                            myGraphic.DrawImage(thumb, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);

                            //Save the Croped and Resized image as a new square thumnail 
                            myBitmapCropped.Save(savePath, codecInfo, encoderParams);
                        }
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

前两项是最重要的,第三项和第四项都是关于代码可读性的:<​​/ p>

  1. 添加输入参数验证
  2. 考虑如果找不到任何路径参数,方法应该如何表现
  3. 您有多个((decimal)squareSize)((decimal)width)的强制转型,考虑单投并将其存储在新的方法级别变量中
  4. 您可以将widthheight的方法默认值的开头设置为等于squareSize,然后删除以下代码块:
  5.    else
       {
          width = squareSize;
          height = squareSize;
       }