BMP to JPG (again)

B

Boris Nienke

Hi,

is there a function available (C# wrapper or something) to convert BMP file
to a JPG file?
i think i ask this once a year at least ;-) but i'm still hoping :)

thanks a lot

Boris
 
B

Boris Nienke

Hi,

is there a function available (C# wrapper or something) to convert BMP file
to a JPG file?
i think i ask this once a year at least ;-) but i'm still hoping :)

Really no solution yet available?
Hard to believe....

Boris
 
B

Boris Nienke

yes - that's what i find whenever i start a new search about this topic.

But isn't there anyone who has done this allready?
A DLL or something with some wrapper-class to do this with C#?

I'm not a C/C++ guru - so writing a "wrapper around IJG" is not that easy
for me.

I'm still hoping that someone out there has done this successfully and like
to share <hope hope hope> :)

Boris
 
B

Boris Nienke

Thanks a lot!
I will look deeper into it when i'm back home.

Currently i wonder what "SHLoadImageFile" exactly is/does and if it's
possible to do convertion to JPG with an in-memory bitmap. I will see

Boris
 
S

Sergey Bogdanov

I was using SHLoadImageFile [1] just for simplicity. It loads image file
and returns its HBITMAP which I pass to WriteBitmapIntoJpegFile.

To convert byte array with image into JPG file you have to use
WriteRGBBytesIntoJpegFile P/Invoke. The last parameter is an array with
length equals to height * width * 3 (where 3 bytes is RGB). I have
updated JpegTest [2] now it also demonstrates how to use
WriteRGBBytesIntoJpegFile function.

[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/apippc/html/ppc_aygshell_thcu.asp
[2]
http://www.sergeybogdanov.com/Samples/JpegTest.zip

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
B

Boris Nienke

Cool, thank you!

Question:
---------
MSDN says about "SHLoadImageFile": [...]Converts a .gif file to a
bitmap.[...]
Now i know it also can load a BMP :)

How did you know about this function(s) if you just knew, that you like to
load a BMP but didn' know if there's a MS-DLL available with some function
to do that?

And: my biggest problems with P/Invoke is, that i don't know which .Net
type/class is compatible to some C++ declaration/parameter in the
parameters. Is there something like a list or a documentation about this?

Boris

I was using SHLoadImageFile [1] just for simplicity. It loads image file
and returns its HBITMAP which I pass to WriteBitmapIntoJpegFile.

To convert byte array with image into JPG file you have to use
WriteRGBBytesIntoJpegFile P/Invoke. The last parameter is an array with
length equals to height * width * 3 (where 3 bytes is RGB). I have
updated JpegTest [2] now it also demonstrates how to use
WriteRGBBytesIntoJpegFile function.

[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/apippc/html/ppc_aygshell_thcu.asp
[2]
http://www.sergeybogdanov.com/Samples/JpegTest.zip

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


Boris said:
Thanks a lot!
I will look deeper into it when i'm back home.

Currently i wonder what "SHLoadImageFile" exactly is/does and if it's
possible to do convertion to JPG with an in-memory bitmap. I will see

Boris
 
S

Sergey Bogdanov

See below.

Boris said:
How did you know about this function(s) if you just knew, that you like to
load a BMP but didn' know if there's a MS-DLL available with some function
to do that?
Have you tried to google:
http://groups-beta.google.com/group...owsce.embedded.vc&q=How+to+load+bitmap&qt_g=1
And: my biggest problems with P/Invoke is, that i don't know which .Net
type/class is compatible to some C++ declaration/parameter in the
parameters. Is there something like a list or a documentation about this?

Types that have a common representation in unmanaged code and CF.NET
have name blittable types. Here you can find the more information about it:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfintrointerp.asp


Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
B

Boris Nienke

B

Boris Nienke

I was using SHLoadImageFile [1] just for simplicity. It loads image file
and returns its HBITMAP which I pass to WriteBitmapIntoJpegFile.

To convert byte array with image into JPG file you have to use
WriteRGBBytesIntoJpegFile P/Invoke. The last parameter is an array with
length equals to height * width * 3 (where 3 bytes is RGB). I have
updated JpegTest [2] now it also demonstrates how to use
WriteRGBBytesIntoJpegFile function.

It works great! THANKS A LOT!!!!!
Still havn't tried the in-memory way, but converting existing BMP files
ist really nice!

Boris
 
G

Guest

I have the same problem , i need convert a BMP into a JPG , i translate the
code of sergey , but a have several error and crash when convert the
images, any idea , any solution ,i'm desesperate.

Best Regards

Bmp_2_JPG_From_File("test.bmp","test.jpg")
Private Sub Bmp_2_JPG_From_File(ByVal cFicheroIn As String, ByVal
cFicheroOut As String)

Dim bmp As IntPtr = SHLoadImageFile(cFicheroIn)

ARM_WriteBitmapIntoJpegFile(cFicheroOut, 90, bmp)

End Sub

<DllImport("coredll.dll", SetLastError:=True)> _

Private Shared Function LoadLibrary(ByVal dllFile As String) As IntPtr

End Function

<DllImport("coredll.dll", SetLastError:=True)> _

Private Shared Function FreeLibrary(ByVal handle As IntPtr) As Boolean

End Function

<DllImport("aygshell.dll", EntryPoint:="#75", SetLastError:=True)> _

Public Shared Function SHLoadImageFile(ByVal szFileName As String) As IntPtr

End Function

<DllImport("coredll", SetLastError:=True)> _

Public Shared Function DeleteObject(ByVal hObject As IntPtr) As IntPtr

End Function

<DllImport("ARM_JpegLibThunk.dll", EnTryPoint:="WriteBitmapIntoJpegFile")> _

Private Function ARM_WriteBitmapIntoJpegFile(ByVal outFile As String, ByVal
quality As Integer, ByVal bitmap As IntPtr) As Boolean

End Function

<DllImport("ARM_JpegLibThunk.dll", EntryPoint:="WriteRGBBytesIntoJpegFile")>
_

Private Function ARM_WriteRGBBytesIntoJpegFile(ByVal outFile As String,
ByVal width As Integer, ByVal height As Integer, ByVal quality As Integer,
ByVal array As Byte) As Boolean

End Function





Boris Nienke said:
I was using SHLoadImageFile [1] just for simplicity. It loads image file
and returns its HBITMAP which I pass to WriteBitmapIntoJpegFile.

To convert byte array with image into JPG file you have to use
WriteRGBBytesIntoJpegFile P/Invoke. The last parameter is an array with
length equals to height * width * 3 (where 3 bytes is RGB). I have
updated JpegTest [2] now it also demonstrates how to use
WriteRGBBytesIntoJpegFile function.

It works great! THANKS A LOT!!!!!
Still havn't tried the in-memory way, but converting existing BMP files
ist really nice!

Boris
 
S

Sergey Bogdanov

Can you provide more information about what error you got?

Also I've noticed that you are calling ARM-compiled library. Did you try
to run it in emulator? If so, you have to replace it with X86 version
for emulator execution. See my example how I detect it at runtime.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


I have the same problem , i need convert a BMP into a JPG , i translate the
code of sergey , but a have several error and crash when convert the
images, any idea , any solution ,i'm desesperate.

Best Regards

Bmp_2_JPG_From_File("test.bmp","test.jpg")
Private Sub Bmp_2_JPG_From_File(ByVal cFicheroIn As String, ByVal
cFicheroOut As String)

Dim bmp As IntPtr = SHLoadImageFile(cFicheroIn)

ARM_WriteBitmapIntoJpegFile(cFicheroOut, 90, bmp)

End Sub

<DllImport("coredll.dll", SetLastError:=True)> _

Private Shared Function LoadLibrary(ByVal dllFile As String) As IntPtr

End Function

<DllImport("coredll.dll", SetLastError:=True)> _

Private Shared Function FreeLibrary(ByVal handle As IntPtr) As Boolean

End Function

<DllImport("aygshell.dll", EntryPoint:="#75", SetLastError:=True)> _

Public Shared Function SHLoadImageFile(ByVal szFileName As String) As IntPtr

End Function

<DllImport("coredll", SetLastError:=True)> _

Public Shared Function DeleteObject(ByVal hObject As IntPtr) As IntPtr

End Function

<DllImport("ARM_JpegLibThunk.dll", EnTryPoint:="WriteBitmapIntoJpegFile")> _

Private Function ARM_WriteBitmapIntoJpegFile(ByVal outFile As String, ByVal
quality As Integer, ByVal bitmap As IntPtr) As Boolean

End Function

<DllImport("ARM_JpegLibThunk.dll", EntryPoint:="WriteRGBBytesIntoJpegFile")>
_

Private Function ARM_WriteRGBBytesIntoJpegFile(ByVal outFile As String,
ByVal width As Integer, ByVal height As Integer, ByVal quality As Integer,
ByVal array As Byte) As Boolean

End Function





I was using SHLoadImageFile [1] just for simplicity. It loads image file
and returns its HBITMAP which I pass to WriteBitmapIntoJpegFile.

To convert byte array with image into JPG file you have to use
WriteRGBBytesIntoJpegFile P/Invoke. The last parameter is an array with
length equals to height * width * 3 (where 3 bytes is RGB). I have
updated JpegTest [2] now it also demonstrates how to use
WriteRGBBytesIntoJpegFile function.

It works great! THANKS A LOT!!!!!
Still havn't tried the in-memory way, but converting existing BMP files
ist really nice!

Boris
 
G

Guest

Thanks Sergey for your time ,

Best Regards





Sergey Bogdanov said:
Can you provide more information about what error you got?

Also I've noticed that you are calling ARM-compiled library. Did you try
to run it in emulator? If so, you have to replace it with X86 version
for emulator execution. See my example how I detect it at runtime.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


I have the same problem , i need convert a BMP into a JPG , i translate the
code of sergey , but a have several error and crash when convert the
images, any idea , any solution ,i'm desesperate.

Best Regards

Bmp_2_JPG_From_File("test.bmp","test.jpg")
Private Sub Bmp_2_JPG_From_File(ByVal cFicheroIn As String, ByVal
cFicheroOut As String)

Dim bmp As IntPtr = SHLoadImageFile(cFicheroIn)

ARM_WriteBitmapIntoJpegFile(cFicheroOut, 90, bmp)

End Sub

<DllImport("coredll.dll", SetLastError:=True)> _

Private Shared Function LoadLibrary(ByVal dllFile As String) As IntPtr

End Function

<DllImport("coredll.dll", SetLastError:=True)> _

Private Shared Function FreeLibrary(ByVal handle As IntPtr) As Boolean

End Function

<DllImport("aygshell.dll", EntryPoint:="#75", SetLastError:=True)> _

Public Shared Function SHLoadImageFile(ByVal szFileName As String) As IntPtr

End Function

<DllImport("coredll", SetLastError:=True)> _

Public Shared Function DeleteObject(ByVal hObject As IntPtr) As IntPtr

End Function

<DllImport("ARM_JpegLibThunk.dll", EnTryPoint:="WriteBitmapIntoJpegFile")> _

Private Function ARM_WriteBitmapIntoJpegFile(ByVal outFile As String, ByVal
quality As Integer, ByVal bitmap As IntPtr) As Boolean

End Function

<DllImport("ARM_JpegLibThunk.dll", EntryPoint:="WriteRGBBytesIntoJpegFile")>
_

Private Function ARM_WriteRGBBytesIntoJpegFile(ByVal outFile As String,
ByVal width As Integer, ByVal height As Integer, ByVal quality As Integer,
ByVal array As Byte) As Boolean

End Function





On Wed, 27 Apr 2005 12:37:17 +0300, Sergey Bogdanov wrote:


I was using SHLoadImageFile [1] just for simplicity. It loads image file
and returns its HBITMAP which I pass to WriteBitmapIntoJpegFile.

To convert byte array with image into JPG file you have to use
WriteRGBBytesIntoJpegFile P/Invoke. The last parameter is an array with
length equals to height * width * 3 (where 3 bytes is RGB). I have
updated JpegTest [2] now it also demonstrates how to use
WriteRGBBytesIntoJpegFile function.

It works great! THANKS A LOT!!!!!
Still havn't tried the in-memory way, but converting existing BMP files
ist really nice!

Boris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top