minibirken

Hello Community!

I'm quite new in VB (just since November06 ) and I'm looking for a fast way to overlap greyscale bitmaps in VB2005 (i.e. for every pixel R=G=B). I tryed to use the alpha-blending tchnique but it doesn't fit my porpouses (after I perform the C. O. the overlapping region is darkned).
I would like the overlapping process to be a lightening process (the resultant color is always at least as light as either of the two costituent colors). In particular overlapping any color with white should produce white. Overlapping any color with black should leave the original color unchanged.
I found an interesting program in VB6 that seems to to that


http://www.vbaccelerator.com/home/VB/Code/vbMedia/Image_Processing/Compositing/article.asp

It implements 24 standard C.O. (Porter-Duff C. O.) and it includes a ready-to-run .exe demo to see what they actually do (the "screen" C.O. seems to perform my task, if you want to have a look..).
Unfortunatly I'm working in VB2005 and I'm not able to convert the code (I tryed with some "conversion-programs" already). I could try to completley re-write it but I just a beginner and the VB6 sorce code seems complicated.

If anyone already came to a similar problem could please help me

Thanx, see you!



Re: Visual Basic Express Edition VB 2005 - Compositing Operations (C.O.)

nobugz

Here's a subroutine that converts a bitmap to gray. It is *not* going to be fast, you might get better performance if you use Bitmap.LockBits, check the docs on how to use it:

Public Shared Sub ConvertToGrayscale(ByVal bmp As Bitmap)
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
Dim c As Color = bmp.GetPixel(x, y)
Dim gray As Integer = (299 * c.R + 587 * c.G + 114 * c.B) \ 1000
bmp.SetPixel(x, y, Color.FromArgb(gray, gray, gray))
Next
Next
End Sub






Re: Visual Basic Express Edition VB 2005 - Compositing Operations (C.O.)

nobugz

Oops, forgot about ColorMatrix. Here's a fast version:

Public Shared Function ConvertToGrayscale(ByVal img As Image) As Image
Dim bmp As New Bitmap(img.Width, img.Height)
Dim gr As Graphics = Graphics.FromImage(bmp)
Dim cm As New Imaging.ColorMatrix(New Single()() { _
New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {0.587, 0.587, 0.587, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0.0, 0.0, 0.0, 1, 0}, _
New Single() {0.0, 0.0, 0.0, 0, 1} _
})
Dim ia As New Imaging.ImageAttributes
ia.SetColorMatrix(cm)
Dim rc As New Rectangle(0, 0, img.Width, img.Height)
gr.DrawImage(img, rc, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
gr.Dispose()
Return bmp
End Function






Re: Visual Basic Express Edition VB 2005 - Compositing Operations (C.O.)

minibirken

Hello, thank you for you answer.

Maybe the Sub you wrote will be of help in future.

But now I just would like to know if in VB2005 there is a way of overlapping bitmaps besides the alpha-blending technique!

With alpha-blending technique I mean the predifined bitmap operations that VB performs after a calling to SetCompositingMode(CompositingMode.SourceCopy/SourceOver).

I know that in VB6 with parameter 'opcode' of Method PaintPicture you couls set many different compositing modes:

-vbBlackness

-vbDstInvert

-vbMergeCopy

-vbMergePaint

-vbNotSrcCopy

...

Is there smth similar in VB2005 too

Thank you very much.





Re: Visual Basic Express Edition VB 2005 - Compositing Operations (C.O.)

nobugz

Sorry, I got side-tracked in your links and forgot about your original question. Best I can do is to still recommend alpha-blending and offer this code...





Re: Visual Basic Express Edition VB 2005 - Compositing Operations (C.O.)

minibirken

Thank you for your code and your advices.

I understood that probably the only way to perform bitmap overlapping ("image compositing") with VB2005 methods/classes is alpha-blending.

Probably next week I'm going to re-write the code I attached before (see my first message) in VB2005 but currently I have to go on writing the thesis. (the program will be my degree dissertation!)

Of course if I'll succeed in doing it I will post the code here.

(as it seems no one has implemented "Porter-Duff Compositing Operation" in VB2005 yet).

Regards





Re: Visual Basic Express Edition VB 2005 - Compositing Operations (C.O.)

nobugz

Look at the Region class to calculate the clipping area for each of the bitmaps.