2016-09-19 79 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

相關問題