Robby
12/13/2004 5:14:00 AM
Typos
(1)
Dim ia As New ImageAttribitues()
*should be
Dim ia As ImageAttributes = New ImageAttributes()
(2)
'Paint the recolored mask onto the original
grRecolored.DrawImage( _
DirectCast(recoloredMask, Image),
*should be
'Paint the recolored mask onto the original
grRecolored.DrawImage( _
DirectCast(recoloredMask, Image), _
(3)
ReDim recolor(0)
*should be
ReDim recolor(0)
recolor(0) = New ColorMap()
Robby
Edmund : R ~ gmail : com
"Robby" <edmund@not.my.email.com> wrote in message
news:uQqQL9M4EHA.3708@TK2MSFTNGP14.phx.gbl...
>
> Another way of doing this uses the ImageAttributes.SetRemapTable with an
> associated ColorMap array and the Graphics.DrawImage method.
>
> The first thing we need to do is load both bitmaps into Bitmap objects.
>
> Dim picture As New Bitmap(<file name>|<image object>)
> Dim mask as New Bitmap(<file name>|<image object>)
>
> To continue we need to know the colors that represent transparent and
> opaque. If the color representing transparent is not in the original
> image or is not Color.Transparent then we do not have to recolor it. We
> create a ColorMap array with one entry if we do not have to recolor the
> color representing transparent or two entries if we do need to recolor
> that color. Next set the opaque color transparent and if nessesary the
> color representing transparent to some color not Color.Transparent and not
> in the original picture. In this example the opaque color is black and
> the transparent color is white in the transparent mask. After setting up
> the ColorMap array we attach it to an ImageAttributes object selecting
> that the recoloring apply to the bitmap.
>
> Dim recolor() As ColorMap = {New ColorMap(), _
> New ColorMap()}
> 'Recolor opaque to transparent
> recolor(0).OldColor = Color.Black
> recolor(0).NewColor = Color.Transparent
> 'Recolor transparent to Magenta
> recolor(1).OldColor = Color.White
> recolor(1).NewColor = Color.Magenta
> 'Attach recolorings
> Dim ia As New ImageAttribitues()
> ia.SetRemapTable(recolor, ColorAdjustType.Bitmap)
>
> We now need a blank canvus on which to paint the recolored mask. It is
> best to make this the same size as the mask and to Clear it with
> Color.Transparent. Then the paint the new mask to it.
>
> Dim recoloredMask As New Bitmap(mask.Width, _
> mask.Height)
> Dim grRecolored As Graphics = Graphics.FromImage( _
> DirectCast(recoloredMask, Image))
> grRecolored.Clear(Color.Transparent)
> 'Paint recolored mask
> grRecolored.DrawImage(DirectCast(mask, Image), _
> New Rectangle(0, 0, mask.Width, mask.Height), _
> 0, 0, mask.Width, mask.Height, _
> GraphicsUnit.Pixel, ia)
> grRecolored.Dispose()
>
> Now we need to paint the recolored mask on to the original picture. You
> can always do a save with a new file name to preserve both the original
> and the masked image.
>
> grRecolored = Graphics.FromImage( _
> DirectCast(picture, Image))
> 'Paint the recolored mask onto the original
> grRecolored.DrawImage( _
> DirectCast(recoloredMask, Image),
> New Rectangle(0, 0, _
> recoloredMask.Width, _
> recoloredMask.Height))
> grRecolored.Dispose()
>
> Finally we recolor the color representing transparent to
> Color.Transparent. We once again need a blank canvus to do this.
>
> 'Recolor transparent color to real transparent
> ReDim recolor(0)
> recolor(0).OldColor = Color.Magenta
> recolor(0).NewColor = Color.Transparent
> ia.SetRemapTable(recolor, ColorAdjustType.Bitmap)
> 'New canvus
> Dim maskedPicture As New Bitmap( _
> picture.Width, picture.Height)
> grRecolored = Graphics.FromImage( _
> DirectCast(maskedPicture , Image))
> grRecolored.Clear(Color.Transparent)
> 'Paint masked picture
> grRecolored.DrawImage(DirectCast(picture, Image), _
> New Rectangle(0, 0, picture.Width, _
> picture.Height), 0, 0, picture.Width, _
> picture.Height, GraphicsUnit.Pixel, ia)
> grRecolored.Dispose()
>
> Remember that the color that you choose to represent transparent in the
> recolored mask must not appear in the picture or you will have unwanted
> pixels (or entire regions) set to transparent that you do not want
> transparent. I have tested this on a .jpg file using a 24bit .bmp the
> same size as the transparent mask and it works as expected. I suggest
> that you always use a .bmp as the transparent mask as I have found the
> compression in other formats blurs the edges of the transparency shapes.
>
> It should be fairly easy to convert this to C# as the only VB specific
> statement is the ReDim on recolor at the end.
>
> Hope this helps
>
> Robby
>
> Edmund : R ~ gmail : com
>
>
>
> "Paul Welter" <loresoft@msdn.nospam.com> wrote in message
> news:uW8Bw2j3EHA.3336@TK2MSFTNGP11.phx.gbl...
>> Hi,
>>
>> I need some help in how to get started with creating a transparent image
>> from two bitmaps. The first image is the image to display, the second
>> image is a mask of the first image for the area that should be
>> transparent. How do I combine the two images to form a gif or tiff that
>> has the mask as an alpha channel for what is transparent in a single
>> image?
>>
>> thanks
>> Paul
>>
>
>