2016-09-19 81 views
0

我在Windows窗体中工作。我有一个图像。它的尺寸是960 * 1280。 enter image description here图像在添加c之前得到调整大小#

当我试图在运行时将此图像添加到我的图片框中。图像正在旋转,图像的大小为1280 * 960。

enter image description here

我的目的是将图像尺寸调整到100 * 100,然后添加到图片框。我不希望那个图像旋转。你能告诉我一些想法吗?

+0

因为有'宽度',然后有'PixelWidth'。请参阅下面的 –

+1

[为什么此位图图像在我加载后更改其大小?]可能的重复(http://stackoverflow.com/questions/39127866/why-this-bitmap-image-changes-its-size-after-i -load-it) –

+1

图像中是否有可能指示旋转的EXIF数据? –

回答

0

把这个在类文件中,并使用下面的大小调整代码

public class ImageResizer 
{ 
    private int allowedFileSizeInByte; 
    private string sourcePath; 
    private string destinationPath; 

    public ImageResizer() 
    { 

    } 

    public ImageResizer(int allowedSize, string sourcePath, string destinationPath) 
    { 
     allowedFileSizeInByte = allowedSize; 
     this.sourcePath = sourcePath; 
     this.destinationPath = destinationPath; 
    } 

    public void ScaleImage() 
    { 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (FileStream fs = new FileStream(sourcePath, FileMode.Open)) 
      { 
       Bitmap bmp = (Bitmap)Image.FromStream(fs); 
       SaveTemporary(bmp, ms, 100); 

       while (ms.Length < 0.9 * allowedFileSizeInByte || ms.Length > allowedFileSizeInByte) 
       { 
        double scale = Math.Sqrt((double)allowedFileSizeInByte/(double)ms.Length); 
        ms.SetLength(0); 
        bmp = ScaleImage(bmp, scale); 
        SaveTemporary(bmp, ms, 100); 
       } 

       if (bmp != null) 
        bmp.Dispose(); 
       SaveImageToFile(ms); 
      } 
     } 
    } 

    public byte[] GetScaledImage(int allowedSize, string sourcePath) 
    { 
     allowedFileSizeInByte = allowedSize; 
     this.sourcePath = sourcePath; 
     //this.destinationPath = destinationPath; 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (FileStream fs = new FileStream(sourcePath, FileMode.Open)) 
      { 
       Bitmap bmp = (Bitmap)Image.FromStream(fs); 
       SaveTemporary(bmp, ms, 100); 

       while (ms.Length < 0.9 * allowedFileSizeInByte || ms.Length > allowedFileSizeInByte) 
       { 
        double scale = Math.Sqrt((double)allowedFileSizeInByte/(double)ms.Length); 
        ms.SetLength(0); 
        bmp = ScaleImage(bmp, scale); 
        SaveTemporary(bmp, ms, 100); 
       } 

       if (bmp != null) 
        bmp.Dispose(); 

       Byte[] buffer = null; 

       if (ms != null && ms.Length > 0) 
       { 
        ms.Position = 0; 
        buffer = new byte[ms.Length]; 
        ms.Read(buffer, 0, buffer.Length); 
       } 

       return buffer; 
      } 
     } 
    } 

    private void SaveImageToFile(MemoryStream ms) 
    { 
     byte[] data = ms.ToArray(); 

     using (FileStream fs = new FileStream(destinationPath, FileMode.Create)) 
     { 
      fs.Write(data, 0, data.Length); 
     } 
    } 

    private void SaveTemporary(Bitmap bmp, MemoryStream ms, int quality) 
    { 
     EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); 
     var codec = GetImageCodecInfo(); 
     var encoderParams = new EncoderParameters(1); 
     encoderParams.Param[0] = qualityParam; 

     if (codec != null) 
      bmp.Save(ms, codec, encoderParams); 
     else 
      bmp.Save(ms, GetImageFormat()); 
    } 

    public Bitmap ScaleImage(Bitmap image, double scale) 
    { 
     int newWidth = (int)(image.Width * scale); 
     int newHeight = (int)(image.Height * scale); 

     Bitmap result = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb); 
     result.SetResolution(image.HorizontalResolution, image.VerticalResolution); 

     using (Graphics g = Graphics.FromImage(result)) 
     { 
      g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      g.CompositingQuality = CompositingQuality.HighQuality; 
      g.SmoothingMode = SmoothingMode.HighQuality; 
      g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
      g.DrawImage(image, 0, 0, result.Width, result.Height); 
     } 
     return result; 
    } 

    private ImageCodecInfo GetImageCodecInfo() 
    { 
     FileInfo fi = new FileInfo(sourcePath); 

     switch (fi.Extension) 
     { 
      case ".bmp": return ImageCodecInfo.GetImageEncoders()[0]; 
      case ".jpg": 
      case ".jpeg": return ImageCodecInfo.GetImageEncoders()[1]; 
      case ".gif": return ImageCodecInfo.GetImageEncoders()[2]; 
      case ".tiff": return ImageCodecInfo.GetImageEncoders()[3]; 
      case ".png": return ImageCodecInfo.GetImageEncoders()[4]; 
      default: return null; 
     } 
    } 

    private ImageFormat GetImageFormat() 
    { 
     FileInfo fi = new FileInfo(sourcePath); 

     switch (fi.Extension) 
     { 
      case ".jpg": return ImageFormat.Jpeg; 
      case ".bmp": return ImageFormat.Bmp; 
      case ".gif": return ImageFormat.Gif; 
      case ".png": return ImageFormat.Png; 
      case ".tiff": return ImageFormat.Tiff; 
      default: return ImageFormat.Png; 
     } 
    } 
} 

这里是调整图像大小

byte[] compressedBuffer = new ImageResizer().GetScaledImage(300000, FileName); 

这里30000显示了大小不同的代码,并且文件名是名字

+0

请注意,这将需要处理能力。原始问题可以通过正确使用加载的图像或ImageControl本身来解决 –

+0

我的窗口ImageResizer类找不到。 – user2115618

+0

好吧,我忘了粘贴imge resizer类 –

0

其中Bitmap类构造函数将原始图像和新大小作为输入:

Image uploadedImage = new Bitmap("Path/to/your/image.bmp"); 
Image resizedImage = new Bitmap(uploadedImage, new Size(100, 100)); 

在样本的第一行中加载图像。在示例的第二行中,使用上传的图像和新尺寸创建一个新对象。

+0

这是我用来上传/添加代码的代码。检查我的问题 – user2115618

+0

是的,但是在上传图片后,您可以再次使用Bitmap对象来调整图片的大小。 – PiotrWolkowski

相关问题