[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
Go Back  Xtreme Visual Basic Talk > > > [GDI+] Create 24 bit bitmap


Reply
 
Thread Tools Display Modes
  #1  
Old 09-14-2015, 10:25 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
Default [GDI+] Create 24 bit bitmap


How can I create a 24 bit bitmap using GDI+

The default bitmap created is 32 bit, but this is causing problems in other parts of my program.

It can be done per MSDN https://msdn.microsoft.com/en-us/lib...13(VS.85).aspx
Reply With Quote
  #2  
Old 09-14-2015, 10:33 AM
OnErr0r's Avatar
OnErr0r[GDI+] Create 24 bit bitmap OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Should be able to just use the correct pixelformat.

PixelFormat24bppRGB
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #3  
Old 09-14-2015, 11:26 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
Default

That's what I thought, but flat api CreateBitmap function doesn't have pixelformat as one of it's parameters

stat = GdipCreateBitmapFromGraphics(800, 1035, gfxPage1, bmpPage1)

width, height, graphics, and bitmap only
Reply With Quote
  #4  
Old 09-14-2015, 01:46 PM
passel's Avatar
passel[GDI+] Create 24 bit bitmap passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

I don't work with the flat API, but I took a quick look and was surprised I couldn't find a simple GdipCreateBitmap call that allowed passing a format.
I assume that if you use GdipCreateBitmapFromGraphics, then the Graphics object passed would have had to been created from a bitmap (or image) that was 24-bit.

Perhaps use the GdipCloneBitmapArea call to convert the source bitmap you created into another bitmap, but with 24-bit bitmap.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 09-14-2015, 04:22 PM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
Default

OK, thanks for all suggestions.
Because of your comment "... that allowed passing a format."
I searched all gdip functions that had PixelFormats as a parameter.

I found GdipBitmapConvertFormat
It also requires four other parameters that I know nothing about.

On a whim, I tried passing zeros for all those parameters, and I think it works!!
If I open the bitmap in Paint it looks OK, file properties report bit depth of 24, and file size is smaller 2,426 KB vs 3,235 KB

Question, have I mucked up the bitmap header somehow by doing this? ...and this could have implications that I am not aware?

Also, how come I can't simply pass 0 instead of cp(0)? VB doesn't like the 0
Is there a way to pass 0 instead? ie vbNull etc?


I used this code:
Code:
    '-------------------------------------------------------------------------------------------
    ' Convert default format (PixelFormat32bppARGB) to another format (PixelFormat24bppRGB)
    
    Dim cp(0) As ColorPalette
    
    stat = GdipBitmapConvertFormat(bmpBackBuffer, PixelFormat24bppRGB, 0, 0, cp(0), 0)
    '-------------------------------------------------------------------------------------------
With this declare:
Code:
Private Declare Function GdipBitmapConvertFormat Lib "gdiplus" _
    (ByVal image As Long, _
     ByVal Format As PixelFormats, _
     ByVal ADitherType As DitherType, _
     ByVal APaletteType As PaletteType, _
     ByRef palette As ColorPalette, _
     ByVal alphaThresholdPercent As Single) As Long
Reply With Quote
  #6  
Old 09-14-2015, 04:57 PM
OnErr0r's Avatar
OnErr0r[GDI+] Create 24 bit bitmap OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

CreateFromScan0 to create one from scratch with a specific PixelFormat. SaveToFile should then save in the same PixelFormat.

vbNull is a constant (1) and has nothing to do with APIs.

When you want to pass a null pointer or a color palette via a function the declared parameter should be ByRef foo As Any. You then pass ByVal 0& or the first item of the array.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #7  
Old 09-14-2015, 05:11 PM
OnErr0r's Avatar
OnErr0r[GDI+] Create 24 bit bitmap OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Btw, this isn't the first time we've discussed null pointers.

http://www.xtremevbtalk.com/1403848-post32.html
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #8  
Old 09-14-2015, 11:54 PM
passel's Avatar
passel[GDI+] Create 24 bit bitmap passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

I saw GdipBitmapConvertFormat, but all the extra parameters made me think that cloning, while taking an extra step with an additional bitmap, might be the safer/easier method.
I also saw the CreateFromScan0 and thought that might be an option, but was thinking you would have to have an array of "pixels" to pass to the function. But, perhaps that isn't required, or is simple to provide a zero filled array to meet the requirement.
I was figuring OnErr0r would be back to set things straight.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #9  
Old 09-15-2015, 08:19 AM
OnErr0r's Avatar
OnErr0r[GDI+] Create 24 bit bitmap OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

CreateFromScan0 can be used with an array of pixels or you can simply pass a null pointer for data and it will allocate the necessary memory and return (byref param) a pointer to it.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #10  
Old 09-15-2015, 08:31 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
Default

I got GdipCreateBitmapFromScan0 to work two ways as Method B below.
Both ways yield the exact same size bitmap 2,426 KB
It looks, as though stride isn't a required parameter.

I've adjusted Method A per your instruction and it creates a 2,426 KB bitmap also.
I guess the header isn't "mucked up" after all.

Last question then...

When and why use 0 or 0& as api parameters?
What's the difference between the two?
0's if ByVal and 0&'s if ByRef? (that's what I did)

BTW, thanks again for your help!!

Method B
Code:
Private Declare Function GdipCreateBitmapFromScan0 Lib "gdiplus" _
    (ByVal width As Long, _
     ByVal height As Long, _
     ByVal stride As Long, _
     ByVal PixelFormat As Long, _
     scan0 As Any, _
     bitmap As Long) As GpStatus

    '-------------------------------------------------------------------------------------------
    ' Create memory bitmap object DIRECTLY to desired pixel format (PixelFormat24bppRGB)
    
    stat = GdipCreateBitmapFromScan0(800, 1035, 800 * 3, PixelFormat24bppRGB, ByVal 0&, bmpBackBuffer)
    'or
    'stat = GdipCreateBitmapFromScan0(800, 1035, 0, PixelFormat24bppRGB, ByVal 0&, bmpBackBuffer)
    '-------------------------------------------------------------------------------------------
Method A
Code:
Private Declare Function GdipBitmapConvertFormat Lib "gdiplus" _
    (ByVal image As Long, _
     ByVal Format As PixelFormats, _
     ByVal ADitherType As DitherType, _
     ByVal APaletteType As PaletteType, _
     ByRef palette As Any, _
     ByVal alphaThresholdPercent As Single) As Long

    '-------------------------------------------------------------------------------------------
    ' Convert default format (PixelFormat32bppARGB) to another format (PixelFormat24bppRGB)
    
    stat = GdipBitmapConvertFormat(bmpBackBuffer, PixelFormat24bppRGB, 0, 0, 0&, 0)
    '-------------------------------------------------------------------------------------------
Reply With Quote
  #11  
Old 09-15-2015, 01:00 PM
OnErr0r's Avatar
OnErr0r[GDI+] Create 24 bit bitmap OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

Constants in VB default to a 16 bit integer. When you want to pass a null pointer 32 bits are required, so need to specify the long data type (&).
__________________
Quis custodiet ipsos custodues.
Reply With Quote
Reply

Tags
bit, bitmap, create, msdn, program, [gdi+]


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap [GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
 
[GDI+] Create 24 bit bitmap
[GDI+] Create 24 bit bitmap
 
-->