-1

My bitmap is too large for uploading to the printer. I am going to compress it to smaller size so that less data will be transmit over the printer. But I don't want reduce the length and width of the bitmap. I have done some research but all of them require a stream especially as following

    bitmap.compress(Bitmap.Format.jpeg,50,outputStream);

Why do I need a stream to store the file? How can I skip that and get the compressed bitmap that I want? I have tried

    originalBitmap = Bitmap.decodeByteArray(imageByteData);
    //Line below not working and got error
    compressedBitmap = Bitmap.compress(Bitmap.Format.jpeg,50,outputStream);

In the outputStream which is my Download folder, I did see the compressed image, but how can I access the compressed image again? Unfortunately, the compress method is not that straight forward. My question is how can I compress a bitmap and use the compressed bitmap in another action? Thank you.

Sheng Jie
  • 141
  • 3
  • 16
  • Take a look [at this previous answer](https://stackoverflow.com/a/19565291/12059568) – Mikael Nov 13 '19 at 15:16
  • 1
    Very unclear. How is _your bitmap too large for uploading to the printer._? Time? Resulting output? Too much data you don't actually need? - Stream or not has nothing to do with it. – TaW Nov 13 '19 at 15:17
  • The answer to your question is "use Bitmap.Compress" which you are already doing. It seems like your core issue is that you don't understand how I/O works in C#. If you write your compressed bitmap to a filestream, then just open that File to read the resulting output. – Jason Nov 13 '19 at 15:36
  • Hi @TaW, my bitmap is almost 3MB, when I debug, the line of the code to send data to the printer takes very long time to be completed before going to next line. – Sheng Jie Nov 14 '19 at 04:00
  • We couldn't see the details of your code, Could you please post a basic demo so that we can help you better? – Jessie Zhang -MSFT Nov 19 '19 at 08:15

1 Answers1

0

You can compress it to an in-memory stream:

//Compress to a stream in memory
byte[] compressedData = null;
using(var stream = new MemoryStream())
{
    bitmap.Compress(Bitmap.Format.Jpeg, 50, stream);
    compressedData = stream.ToArray();
}

//load compressed bitmap from memory
using(var stream = new MemoryStream(compressedData))
{
    var compressedBitmap = BitmapFactory.DecodeStream(stream);
}
Alberto
  • 15,626
  • 9
  • 43
  • 56