2016-05-31 123 views
0

我有這種方法,我想從磁盤加載一張圖片,然後調整它的大小,並將其保存到一個字節數組,將其保存到一個mysql表中的blob。至於我可以告訴它正在調整大小,但是當我將它保存到表中時,我可以看到字節數組是原始文件大小。但我只想保存表格中的縮略圖。調整大小BitmapImage

yelloDataSet.EmployeesPicture[index].Picture = 
    helperClass.LoadImageInToByteArray(helperClass.OpenFileDialogSingle("Abrir Imaginen."),150,150); 

我在這裏缺少什麼:

/// <summary> 
    /// Load and resize image into a byte array. 
    /// </summary> 
    /// <param name="bitmap"></param> 
    /// <param name="FileName"></param> 
    /// <param name="Width"></param> 
    /// <param name="Height"></param> 
    /// <returns></returns> 
    public Byte[] LoadImageInToByteArray(string FileName, int Width, int Height) 
    { 
     BitmapImage bitmap = new BitmapImage(); 

     System.IO.FileStream photoStream = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); 

     bitmap.BeginInit(); 
     bitmap.StreamSource = photoStream; 
     bitmap.CacheOption = BitmapCacheOption.OnLoad; 
     bitmap.DecodePixelHeight = Height; 
     bitmap.DecodePixelWidth = Width; 
     byte[] buffer = BufferFromImage(bitmap); 
     bitmap.EndInit(); 
     return buffer; 
    } 

    /// <summary> 
    /// Convert BitImage into Byte Array. 
    /// </summary> 
    /// <param name="imageSource"></param> 
    /// <returns></returns> 
    public Byte[] BufferFromImage(BitmapImage imageSource) 
    { 
     byte[] result = null; 

     if (imageSource != null) 
     { 
      using (MemoryStream memStream = new MemoryStream()) 
      { 
       JpegBitmapEncoder encoder = new JpegBitmapEncoder(); 
       encoder.Frames.Add(BitmapFrame.Create(imageSource)); 
       encoder.Save(memStream); 

       result = memStream.ToArray(); 
      } 

     } 

     return result; 
    } 

此功能是通過這條線叫什麼?

+0

[BufferFromImage()](http://stackoverflow.com/a/9847529/1997232)?如果是,那麼很可能不會使用新的位圖寬度或高度,而是基礎流正在複製一對一。 – Sinatr

+0

嘗試在'BufferFromImage()'之前調用'EndInit()'。 – Sinatr

+0

這給了我一個空的字節[] ... –

回答

-1
 public static MemoryStream ResizeImageFromStream(Stream stream, String nameOfFile, int width, int height) 
{ 
    try 
    { 
     Image img; 
     double ratio; 
    int newWidth; 
    int newHeight;  
     Graphics gfx = null; 
     img = Image.FromStream(stream); 
     Image Photo = new Bitmap(img, width, height); 
     Encoder myEncoder; 
     EncoderParameter myEncoderParameter; 
     EncoderParameters myEncoderParameters; 
     ImageCodecInfo myImageCodecInfo; 

     myImageCodecInfo = GetEncoderInfo(nameOfFile.Substring(nameOfFile.LastIndexOf(".")).ToLower()); 
     myEncoder = Encoder.Quality; 
     myEncoderParameters = new EncoderParameters(1); 
     myEncoderParameter = new EncoderParameter(myEncoder, 87L); 
     myEncoderParameters.Param[0] = myEncoderParameter; 
     MemoryStream mstr = new MemoryStream(); 

     gfx = Graphics.FromImage(Photo); 
     gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     gfx.SmoothingMode = SmoothingMode.HighQuality; 
     gfx.PixelOffsetMode = PixelOffsetMode.HighQuality; 
     gfx.CompositingQuality = CompositingQuality.HighQuality; 
     gfx.DrawImage(img, 0, 0, width, height); 

     ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders(); 
     EncoderParameters encParams = new EncoderParameters(1); 
     encParams.Param[0] = new EncoderParameter(Encoder.Quality, m_imageQuality); 

     img.Dispose(); 
     Photo.Save(mstr, info[1],encParams); 
     Photo.Dispose(); 

     return mstr; 

    } 
    catch 
    { 
     return new MemoryStream(); 
    } 


} 

可以的MemoryStream轉換爲字節

+0

這對於WPF應用程序來說沒什麼意義。除此之外,你可以編輯和取消刪除你的其他答案,而不是寫一個新的答案。 – Clemens