Camera to PNG

DTsEMT

Member
Licensed User
Longtime User
I'm using the Camera example to take pictures to a Panel. The example uses the line

out.WriteBytes(Data, 0, Data.Length)

which results in a file type of JPG. I understand from reading through the forums that the JPG and PNG formats are supported natively, and wanted to write PNG files (I suspect they'll be smaller).

The original defines Filename (string) as "0001.jpg",

out = File.OpenOutput(File.DirRootExternal, FileName, False)

So, thinking native support = just rename, I set FileName to "0001.png". It's still a jpg. Hmmm.

The examples I've seen which specify PNG output use .writetostream, but the action isnt supported:

panel1.WriteToStream(out, 100, "PNG")

The camera (from the example Camera.zip) is loading to the panel, thus:

camera1.Initialize(Panel1, "Camera1")

Do I need a Canvas, or load the camera to a bitmap, or...? So confused. Any clarification would be appreciated!

:sign0104:
 

DTsEMT

Member
Licensed User
Longtime User
Solved (I think...)

The fix I found was the following:

Sub Camera1_PictureTaken (Data() As Byte)
Panel1.Enabled = False
camera1.StartPreview
'
' create 00001..00009, 000010, then pull off the last four digits for 0001..0009, 0010..9999
'
PicNumber = PicNumber + 1
FileName = "0000" & PicNumber
FileName = FileName.SubString2(FileName.Length-4, FileName.Length) & ".png"
'
' have to use writetostream to force PNG
' Panel wants to writebytes, will not writetostream
' have to define a bitmap and load the camera data to that first.
' so, define an input stream and load that with the camera data...
'
Dim inp As InputStream
inp.InitializeFromBytesArray(Data,0,Data.Length)
'
' define a bitmap and load that from the input stream...
'
Dim bd As Bitmap
bd.Initialize2(inp)
'
' define the output stream and use the writetostream of the bitmap...

Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, FileName, False)
bd.WriteToStream(out, 100, "PNG")
'
' close the outputstream
'
out.Close

... and that seems to work. Interesting to note that the original JPG file output was 1.8MB while the PNG output is 4.46MB - another issue entirely.

Thanks! :cool:
 
Upvote 0
Top