从中心切割图像后出现错误的尺寸图像

时间:2016-10-17 08:00:28

标签: c# .net image

我正在尝试从中心裁剪图像,并希望生成100 * 46尺寸的图像,但我得到的图像尺寸为46 * 17

代码:

 SaveCroppedImage(Image.FromStream(file.InputStream), 100, 46, "MyfilePath", true);

public void SaveCroppedImage(Image image, UInt16 thumbWidth, UInt16 thumbHeight, string filePath, Boolean checkDimension)
        {
            if (checkDimension && image.Width > image.Height)
            {
                var tmp = thumbHeight;
                thumbHeight = thumbWidth;
                thumbWidth = tmp;
            }

            if (image.Width > thumbWidth || image.Height > thumbHeight)
            {
                float scaleWidth = 1;
                float scaleHeight = 1;
                float scale = 1;
                scaleWidth = ((float)thumbWidth / (float)image.Width);
                scaleHeight = ((float)thumbHeight / (float)image.Height);

                if (scaleWidth < scaleHeight)
                {
                    scale = scaleWidth;
                }
                else
                {
                    scale = scaleHeight;
                }
                thumbWidth = (UInt16)Math.Round(image.Width * scale, 0);
                thumbHeight = (UInt16)Math.Round(image.Height * scale, 0);

            }

            if (thumbWidth > image.Width)
                thumbWidth = (UInt16)image.Width;

            if (thumbHeight > image.Height)
                thumbHeight = (UInt16)image.Height;

            //System.Drawing.Image thumb = image.GetThumbnailImage(thumbWidth, thumbHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            System.Drawing.Image thumb = new System.Drawing.Bitmap(thumbWidth, thumbHeight);
            System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(thumb);

            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
            gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);

            //
            // get raw format
            System.Drawing.Imaging.ImageFormat format = image.RawFormat;

            //
            // get encoder
            System.Drawing.Imaging.ImageCodecInfo info = null;
            foreach (System.Drawing.Imaging.ImageCodecInfo ici in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
            {
                if (System.Drawing.Imaging.ImageFormat.Jpeg.Equals(format))
                {
                    info = ici;
                    break;
                }
                else if (System.Drawing.Imaging.ImageFormat.Gif.Equals(format))
                {
                    info = ici;
                    break;
                }
                else if (System.Drawing.Imaging.ImageFormat.Png.Equals(format))
                {
                    info = ici;
                    break;
                }
                else if (System.Drawing.Imaging.ImageFormat.Bmp.Equals(format))
                {
                    info = ici;
                    break;
                }
                else if (System.Drawing.Imaging.ImageFormat.Tiff.Equals(format))
                {
                    info = ici;
                    break;
                }
                else if (System.Drawing.Imaging.ImageFormat.Wmf.Equals(format))
                {
                    info = ici;
                    break;
                }
            }

            // check if we found the right encoder, otherwise use the rawformat and save
            using (var stream = new MemoryStream())
            {
                if (info == null)
                {
                    thumb.Save(stream, format);
                }
                else
                {
                    //
                    // create parameters and save
                    System.Drawing.Imaging.EncoderParameters encParams = new System.Drawing.Imaging.EncoderParameters(1);
                    encParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                    thumb.Save(stream, info, encParams);
                }

                stream.Seek(0, SeekOrigin.Begin);
                StoreFile(stream, filePath);
                image.Dispose();
            }
            gr.Dispose();
        }

private static Boolean StoreFile(Stream fileStream, String filePath, Boolean overwriteIfExists = true)
        {
            if (fileStream.CanSeek) fileStream.Seek(0, SeekOrigin.Begin);

            var fileWasWritten = false;
            try
            {
                using (var stream = File.Open(filePath, FileMode.Create, FileAccess.Write))
                {
                    if (stream.CanSeek) stream.Seek(0, SeekOrigin.Begin);

                    fileStream.CopyTo(stream);
                }

                fileWasWritten = true;
            }
            catch
            {
                return false;
            }

            return fileWasWritten;
        }

我的示例输入图片:Trial Image

输出图片enter image description here

我正在尝试保存只格式化用户图像的图像。例如:如果用户选择的图像是png格式,那么我想保存图像 仅限png格式,如果图像是jpg格式,则以jpg格式保存图像。

我已尝试过以下链接中的代码,但是来自以下链接的代码会产生质量非常差的图像:

Reference

1 个答案:

答案 0 :(得分:1)

由于图像宽度始终大于高度,因此该条件始终为真

if (checkDimension && image.Width > image.Height)

并切换结果图像的尺寸。

如果您想要检查图像,请执行以下操作:

 var oriantationthumb = (thumbWidth/(float)thumbHeight) > 1;
 var oriantationImage = (imageWidth/(float)imageHeight) > 1;

 if(oriantationthumb != oriantationImage)
 {
  //exchange 
 }
相关问题