fast convert rgb to grayscale

16/11/2010 - 23:02 por flyguille | Informe spam
fast grayscaling a big bitmap trucolor (like 12000y, 9600x pix) in vb6

I already googled a lot, take GDI+ tutorials, and everything. But I
don't want it in the slow way

So, I wondered if that well know VB code about making a colourmatrix
is possible to translate to work in VB5/6?

[CODE]
Dim dlg As OpenFileDialog = New OpenFileDialog()
dlg.Filter = "Image files (*.BMP, *.JPG, *.GIF)|*.bmp;*.jpg;*.gif"
If dlg.ShowDialog() = DialogResult.OK Then
Dim img As Image = Image.FromFile(dlg.FileName)
Dim bm As Bitmap = New Bitmap(img.Width, img.Height)
Dim g As Graphics = Graphics.FromImage(bm)
Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {0.3, 0.3, 0.3, 0, 0}, _
New Single() {0.59, 0.59, 0.59, 0, 0}, _
New Single() {0.11, 0.11, 0.11, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})


Dim ia As ImageAttributes = New ImageAttributes()
ia.SetColorMatrix(cm)
g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0,
0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
g.Dispose()
Me.BackgroundImage = bm
End If

[/CODE]

strip out the img loading lines, i already have the image in a
StdPicture, maybe I can to setup a DC and selecting the bitmap of the
stdpicture but... how to implement that colorMatrix thing in VB6?

anybody has the routine already done? i don't want to reinvent the
whell.
 

Leer las respuestas

#1 Langosta
18/11/2010 - 15:59 | Informe spam
"flyguille" escribió en el mensaje de noticias
news:
fast grayscaling a big bitmap trucolor (like 12000y, 9600x pix) in vb6

I already googled a lot, take GDI+ tutorials, and everything. But I
don't want it in the slow way

So, I wondered if that well know VB code about making a colourmatrix
is possible to translate to work in VB5/6?

[CODE]
Dim dlg As OpenFileDialog = New OpenFileDialog()
dlg.Filter = "Image files (*.BMP, *.JPG, *.GIF)|*.bmp;*.jpg;*.gif"
If dlg.ShowDialog() = DialogResult.OK Then
Dim img As Image = Image.FromFile(dlg.FileName)
Dim bm As Bitmap = New Bitmap(img.Width, img.Height)
Dim g As Graphics = Graphics.FromImage(bm)
Dim cm As ColorMatrix = New ColorMatrix(New Single()() _
{New Single() {0.3, 0.3, 0.3, 0, 0}, _
New Single() {0.59, 0.59, 0.59, 0, 0}, _
New Single() {0.11, 0.11, 0.11, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})


Dim ia As ImageAttributes = New ImageAttributes()
ia.SetColorMatrix(cm)
g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0,
0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
g.Dispose()
Me.BackgroundImage = bm
End If

[/CODE]

strip out the img loading lines, i already have the image in a
StdPicture, maybe I can to setup a DC and selecting the bitmap of the
stdpicture but... how to implement that colorMatrix thing in VB6?

anybody has the routine already done? i don't want to reinvent the
whell.







Digamos que tu imagen es de 24 bits. Recuperas es array de bits
(GetDIBits) y recorres pixel a pixel haciendo el siguiente cálculo

byte_indice = byte_rojo * 0.3 + byte_verde * 0.59 + byte_azul * 0.11

esa es la operación representada en la matriz.

una imagen en escala de grises, es un bitmap indexado de 8 bits

la primer entrada en la paleta es 0, 0, 0, 0
la segunda 1, 1, 1, 0
la última 255, 255, 255, 0

Todo bien.

Preguntas similares