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;
}
此功能是通過這條線叫什麼?
[BufferFromImage()](http://stackoverflow.com/a/9847529/1997232)?如果是,那麼很可能不會使用新的位圖寬度或高度,而是基礎流正在複製一對一。 – Sinatr
嘗試在'BufferFromImage()'之前調用'EndInit()'。 – Sinatr
這給了我一個空的字節[] ... –