Newsgroup: it.comp.lang.visual-basic
Titolo: Picture a 256 colori

E' possibile salvare o caricare una picture "forzando" i 256 colori??


Newsgroups: vb.multimedia_graphics
Subject: Re: reducing colors to 256
Date: Mon, 3 Jan 2000 12:15:58 -0500
Bitmap files start with a fileheader, then an infoheader, followed by the
palette entries (unless it's a 24-bit bmp), and finally the bitmap bits
themselves. The only complication is that the palette and bitmap bits are
Dword-aligned, which means that palette entries are RGBQUADs, and each scan
line of bitmap bits has to have an even multiple of 4 bytes (up to 3 padding
bytes are added if necessary).
I've got a routine here to save a picturebox as a monochrome bitmap: you
should find it pretty easy to change it for 16- and 256-color standard
palettes (if not, just ask more questions here!). Custom palettes are quite
a bit more work...
Oh, and the picturebox must have AutoRedraw = True, ScaleMode = vbPixel (3).
Good luck!
Jim Deutch
MS Dev MVP
Private Type BITMAPFILEHEADER '14 bytes
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO_1
bmiHeader As BITMAPINFOHEADER
bmiColors(1) As RGBQUAD
End Type
Private Const PIXEL As Integer = 3
Private Const DIB_RGB_COLORS As Long = 0
Private Const PALVERSION = &H300
Private Declare Function GetDIBits1 Lib "gdi32" Alias "GetDIBits" _
(ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, _
ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO_1, _
ByVal wUsage As Long) As Long
Private Sub Save1(pic As PictureBox)
Dim SaveBitmapInfo_1 As BITMAPINFO_1
Dim SaveFileHeader As BITMAPFILEHEADER
Dim SaveBits() As Byte
Dim BufferSize As Long
Dim fNum As Long
Dim Retval As Long
Dim nLen As Long
Const BitsPixel = 1
'size a buffer for the pixel data
BufferSize = ((pic.ScaleWidth / 8 + 3) And &HFFFC) * pic.ScaleHeight
ReDim SaveBits(0 To BufferSize - 1)
'fill the header info for the save copy
With SaveBitmapInfo_1.bmiHeader
.biSize = 40
.biWidth = pic.ScaleWidth
.biHeight = pic.ScaleHeight
.biPlanes = 1
.biBitCount = BitsPixel
.biCompression = 0
.biClrUsed = 0
.biClrImportant = 0
.biSizeImage = BufferSize
End With
nLen = Len(SaveBitmapInfo_1)
'get the bitmap from the picturebox
Retval = GetDIBits1(pic.hdc, pic.Image, 0, _
SaveBitmapInfo_1.bmiHeader.biHeight, SaveBits(0), SaveBitmapInfo_1,
DIB_RGB_COLORS)
' create a header for the save file
With SaveFileHeader
.bfType = &H4D42
.bfSize = Len(SaveFileHeader) + nLen + BufferSize
.bfOffBits = Len(SaveFileHeader) + nLen
End With
' save it to disk
fNum = FreeFile
Open m_SaveFileName For Binary As fNum
Put fNum, , SaveFileHeader
Put fNum, , SaveBitmapInfo_1
Put fNum, , SaveBits()
Close fNum
End Sub

GRAZIE!!!!

Rimangio quello che ho detto: non ci riesco!
Qualcuno ha un esempio pronto???
Grazie mille!

 Grazie mille!
non e' molto carino da parte tua, visto che tutto qlo che avevi detto era
"grazie" :)
cmq su vbaccelerator c'e' un esempio funzionante con differenti
ottimizzazioni. non so se e' sufficientemente "pronto"...

 "grazie" :)
Oppps!!!!
Non riesco a trovarlo quel maledetto esempio!!! Sai come si chiama???
Comunque....GRAZIE!!!! :->

SimplyJack <nof...@no.life> wrote


di niente :)
il sito me lo sono scaricato, e da me dovrebbe essere
file:///C:/tp_prg/_vb/vb-accelerator/
www.vbaccelerator.com/codelib/gfx/clrde
pth.zip
se clicchi a sx su code, a dx nella sezione 4 vb media c'e' un solo esempio
di codice per la grafica.
cmq sul codice di deutch ci ho sofferto anch'io. come sotto dovrebbe andare.
se riesco a metterlo a punto mando l'esempio al ng.
ciao, a.
'By default, VB uses the windows "halftone" palette.  If you don't like it,
'use the PaletteMode and Palette properties of your form or, more simply,
set
'the PaletteMode to "UseZOrder" and place an invisible picturebox on your
'form with a 256-color bitmap in it and make sure its at the top of the
'ZOrder with
'Picturex.ZOrder
'in your form_activate.




   bmiColors(256) As RGBQUAD
End Type
Private Const PIXEL As Integer = 3
Private Const DIB_RGB_COLORS As Long = 0
Private Const PALVERSION = &H300
Private Declare Function GetDIBits1 Lib "gdi32" Alias "GetDIBits" _
  (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, _
  ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO_1, _
  ByVal wUsage As Long) As Long
Private Sub Save1(pic As PictureBox)
On Error GoTo errman
    Dim SaveBitmapInfo_1 As BITMAPINFO_1
    Dim SaveFileHeader As BITMAPFILEHEADER
    Dim SaveBits() As Byte
    Dim BufferSize As Long
    Dim fNum As Long
    Dim Retval As Long
    Dim nLen As Long
    Const BitsPixel = 8
    BufferSize = ((pic.ScaleWidth + 3) And &HFFFC) * pic.ScaleHeight
    ReDim SaveBits(0 To BufferSize - 1)
    With SaveBitmapInfo_1.bmiHeader
        .biSize = 40
        .biWidth = pic.ScaleWidth
        .biHeight = pic.ScaleHeight
        .biPlanes = 1
        .biBitCount = BitsPixel
        .biCompression = 0
        .biClrUsed = 0
        .biClrImportant = 0
        .biSizeImage = BufferSize
    End With
    nLen = Len(SaveBitmapInfo_1)
    Retval = GetDIBits1(pic.hDC, pic.Image, 0, _
        SaveBitmapInfo_1.bmiHeader.biHeight, SaveBits(0), _
        SaveBitmapInfo_1, DIB_RGB_COLORS)
    With SaveFileHeader
       .bfType = &H4D42
       .bfSize = Len(SaveFileHeader) + nLen + BufferSize
       .bfOffBits = Len(SaveFileHeader) + nLen
    End With
    fNum = FreeFile
    Open fSave For Binary As fNum 'fsave = file su cui salvi
    Put fNum, , SaveFileHeader
    Put fNum, , SaveBitmapInfo_1
    Put fNum, , SaveBits()
    Close fNum
    Exit Sub
errman:    MsgBox Error
End Sub