Image Output (Bitmap.WriteToStream)

yttrium

Active Member
Licensed User
Longtime User
I have a bitmap and when I set an ImageView to view it in the app, it appears perfectly normal:
http://ompldr.org/vaTZxZw/Screenshot_2013-04-22-14-20-35.png

However, when I export that same bitmap without modification to a file using the WriteToStream method, I get the following:
http://ompldr.org/vaTZxaA/Scrotter4A_20130422_141705.png

For some reason, the image clips along the bottom. When opened with Photoshop, I get the error "Could not complete your request because the file-format module cannot parse the file". Windows 7's default image viewer opens it without issue, save for the clipping issue.

My code is as follows, for writing:
B4X:
Sub SaveBtn_Click
   DateTime.DateFormat = "yyyyMMdd_HHmmss"
   Dim result As Int
   result = Msgbox2("Save file as what format?", "Save Image", "PNG", "Cancel", "JPG", Null)
   Select Case result
      Case DialogResponse.POSITIVE
         Dim filename As String = "Scrotter4A_" & DateTime.Date(DateTime.now) & ".png"
         Dim Out As OutputStream
         Out = File.OpenOutput(File.Combine(File.DirRootExternal, "Scrotter/"), filename, False)
         FinalBitmap.WriteToStream(Out, 100, "PNG")
         ToastMessageShow ("File saved to the sdcard at /sdcard/Scrotter/" & filename & ".", True)
      Case DialogResponse.NEGATIVE
         Dim filename As String = "Scrotter4A_" & DateTime.Date(DateTime.now) & ".jpg"
         Dim Out As OutputStream
         Out = File.OpenOutput(File.Combine(File.DirRootExternal, "Scrotter/"), filename, False)
         FinalBitmap.WriteToStream(Out, 95, "JPEG")
         ToastMessageShow ("File saved to the sdcard at /sdcard/Scrotter/" & filename & ".", True)
   End Select
End Sub

Any ideas?
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Try putting

B4X:
out.flush
out.close

before the end of each case block.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
Try putting

B4X:
out.flush
out.close

before the end of each case block.

Fixed it, thanks!

So that I better understand in the future, what is the actual importance of each action?
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
out.Flush is not really required in this case.

If you do not call out.Close then some of the last bytes will not be written.

And that's exactly what I experienced - the last hundred or so pixels were cut and some image metadata and encoding information was lost, which is what caused Photoshop to crash.
Thanks Erel!
 
Upvote 0
Top