简体   繁体   中英

Converting FileStream to WriteableBitmap to JPEG to Byte Array for SSRS

I'm trying to save an Image to SQL Server so SSRS can read it. I need to convert to WriteableBitmap (and possibly JPEG?) so I can make changes to the image size before saving. However, when I try to pull the converted image out of SQL Server, it will not render in SSRS at all. What am I doing wrong?

  byte[] m_Bytes = ReadToEnd(fileStream); //this works fine
  WriteableBitmap bmp1 = new WriteableBitmap(166, 166);
  bmp1.FromByteArray(m_Bytes);  //this works fine

  ExtendedImage image = bmp1.ToImage();
  MemoryStream stream = new MemoryStream();
  ImageTools.IO.Encoders.AddEncoder<JpegEncoder>();
  JpegEncoder encoder = new JpegEncoder();
  encoder.Encode(image, stream);

  BitmapImage img = new BitmapImage();
  img.SetSource(stream);
  WriteableBitmap bmp2 = new WriteableBitmap(img);


  byte[] buffer1 = bmp2.ToByteArray();
  CurrentOrder.CompanyImage = buffer1; //this does save a byte array but it will not render in SSRS.  If I set buffer1 to bmp1.ToByteArray() then it works fine but I am still unable to resize it using the resize method in WriteableEx without it not rendering in SSRS.

This is another try at the same thing and it won't render either:

And this is simpler and won't work either:

byte[] m_Bytes = ReadToEnd(fileStream); 
WriteableBitmap bmp1 = new WriteableBitmap(166, 166); 
bmp1.FromByteArray(m_Bytes); 
WriteableBitmap resizedImage = bmp1.Resize(25, 25, WriteableBitmapExtensions.Interpolation.Bilinear); 
byte[] buffer1 = resizedImage.ToByteArray(); 
CurrentOrder.CompanyImage = buffer1;

What you want is to resize 166x166 (or 25x25), export as byte[] and reload picture from this byte array ?

Can you try with BitmapImage ?

BitmapImage image = new BitmapImage();
image.SetSource(fileStream);

WriteableBitmap bitmap = new WriteableBitmap(image);
WriteableBitmap resizedBitmap = bitmap.Resize(25, 25, WriteableBitmapExtensions.Interpolation.Bilinear);

CurrentOrder.CompanyImage = resizedBitmap.ToByteArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM