http://www.dotnet247.com/247reference/msgs/28/140412.aspx

Moses Zhao
Hi All,

I've got an image which I would like to change into B/W when drawing it....
what would the ColorMatrix for this be ?

thanks,

moses

Reply to this message...
 
    
Bob Powell (VIP)
Visit the GDI+ FAQ on my site for an answer to this one...

Bob.

--
xRay tools Professional Windows Forms componenets at amazing prices.
http://www.bobpowell.net/xray_tools.htm
Buy the tool and get source code in VB or C#

Buy my book, C# and the .NET framework...
http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

See my book recommendations...
http://www.bobpowell.net/recommendations.htm

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Moses Zhao" <Click here to reveal e-mail address> wrote in message
news:Ot9qHM1bCHA.1656@tkmsftngp11...
[Original message clipped]

Reply to this message...
 
    
Jacob Grass [MVP]
Bob Powell wrote:
[Original message clipped]

Yeah, but that doesn't use a ColorMatrix. The ColorMatrix is:

Dim GrayShear()() As Single = New Single()() _
{New Single(4) {0.5, 0.5, 0.5, 0, 0}, _
New Single(4) {0.5, 0.5, 0.5, 0, 0}, _
New Single(4) {0.5, 0.5, 0.5, 0, 0}, _
New Single(4) {0, 0, 0, 1, 0}, _
New Single(4) {0, 0, 0, 0, 1}}

Dim colMatrix As ColorMatrix = New ColorMatrix(GrayShear)

--
Jacob Grass
Microsoft .NET MVP
Check out http://windowsforms.net

Reply to this message...
 
    
Bob Powell (VIP)
Thanks Jacob,
I added that as an example on my page and credited you..If you'd like me
to add more detail about you or link to another site, let me know. I'll be
happy to change it.
Bob.

--
xRay tools Professional Windows Forms componenets at amazing prices.
http://www.bobpowell.net/xray_tools.htm
Buy the tool and get source code in VB or C#

Buy my book, C# and the .NET framework...
http://www.amazon.com/exec/obidos/ASIN/067232153X/bobpowelnet

See my book recommendations...
http://www.bobpowell.net/recommendations.htm

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Jacob Grass [MVP]" <Click here to reveal e-mail address> wrote in message
news:eUuKKe8bCHA.2524@tkmsftngp10...
[Original message clipped]

Reply to this message...
 
    
Jacob Grass [MVP]
Hi Bob-

[Original message clipped]

No problem at all. . .Thanks for the credit, although, I stole it from Ed
Stegman in this very group many moons ago. . . :-)

Oh, and WindowsForms.net isn't my site, that's an MS site. . . You can
simply mention that I am an MVP and I'll be happy...

However, you may want to post this code [1] for a bitmap color converter
that is much quicker than using GetPixel/SetPixel (and includes a naive
grey-scale, which, based on the luminescence stuff on your site, you should
likely modify to make it more correct) . . .

[1]

Public Class ColorConverter
Private m_Image As Bitmap
Private m_width As Int32
Private m_BitmapData As BitmapData = Nothing
Private pBase As IntPtr = Nothing
Private m_NumPixelsPerRow As Integer
Private m_ColorToChange As Color = Color.Red
Private m_NewColor As Color = Color.Blue

<StructLayout(LayoutKind.Sequential)> _
Public Structure PixelData
Public blue As Byte
Public green As Byte
Public red As Byte
End Structure

Public Sub New(ByVal image As Bitmap)
m_Image = image
End Sub

Public Sub Dispose()
m_Image.Dispose()
End Sub

Public ReadOnly Property Image() As Bitmap
Get
Return m_Image
End Get
End Property

Public Property ColorToChange() As Color
Get
Return m_ColorToChange
End Get
Set(ByVal Value As Color)
m_ColorToChange = Value
End Set
End Property

Public Property NewColor() As Color
Get
Return m_NewColor
End Get
Set(ByVal Value As Color)
m_NewColor = Value
End Set
End Property

Public ReadOnly Property PixelSize() As Point
Get
Dim Unit As GraphicsUnit = GraphicsUnit.Pixel
Dim Bounds As RectangleF = Image.GetBounds(Unit)
Return New Point(CType(Bounds.Width, Int32), _
CType(Bounds.Height, Int32))

End Get
End Property

Public Sub Save(ByVal filename As String)
Image.Save(filename, ImageFormat.Bmp)
End Sub

Public Sub LockBitmap()
Dim Unit As GraphicsUnit = GraphicsUnit.Pixel
Dim boundsF As RectangleF = Image.GetBounds(Unit)
Dim bounds As Rectangle = _
New Rectangle(CType(boundsF.X, Int32), _
CType(boundsF.Y, Int32), _
CType(boundsF.Width, Int32), CType(boundsF.Height, Int32))

m_width = bounds.Width * Marshal.SizeOf(GetType(PixelData))

If m_width Mod 4 <> 0 Then
m_width = CType(4 * (m_width / 4) + 1, Int32)
End If

m_BitmapData = Image.LockBits(bounds, _
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)

pBase = m_BitmapData.Scan0
End Sub

Public Sub UnLockBitmap()
Image.UnlockBits(m_BitmapData)
m_BitmapData = Nothing
pBase = Nothing
End Sub

Public Sub ConvertColor()
Dim size As Point = PixelSize
Dim PixelDataSize As Integer = Marshal.SizeOf(GetType(PixelData))
Dim pixStruct As PixelData
Dim tempPix As IntPtr
Dim RowByteSize As Int32 = size.Y * PixelDataSize
Dim pixBytes(RowByteSize - 1) As Byte
Dim i As Int32
Dim Count As Integer = size.X
Dim temp As Byte

LockBitmap()
tempPix = pBase

Do While Count <> 0
Marshal.Copy(tempPix, pixBytes, 0, RowByteSize)

For i = 0 To RowByteSize - 1 Step 3
If ColorToChange.R = pixBytes(i) AndAlso _
ColorToChange.G = pixBytes(i + 1) AndAlso _
ColorToChange.B = pixBytes(i + 2) Then

pixBytes(i) = NewColor.R
pixBytes(i + 1) = NewColor.G
pixBytes(i + 2) = NewColor.B
End If
Next

Marshal.Copy(pixBytes, 0, tempPix, RowByteSize)

tempPix = IntPtr.op_Explicit(tempPix.ToInt32 + RowByteSize)
Count -= 1
Loop

UnLockBitmap()

End Sub

Public Sub MakeGrey()
Dim size As Point = PixelSize
Dim PixelDataSize As Integer = Marshal.SizeOf(GetType(PixelData))
Dim pixStruct As PixelData
Dim tempPix As IntPtr
Dim RowByteSize As Int32 = size.Y * PixelDataSize
Dim pixBytes(RowByteSize - 1) As Byte
Dim i As Int32

LockBitmap()
tempPix = pBase

Dim Count As Integer = size.X
Dim temp As Byte

Do While Count <> 0
Marshal.Copy(tempPix, pixBytes, 0, RowByteSize)

For i = 0 To RowByteSize - 1 Step 3
temp = CType((CType(pixBytes(i), Short) + _
pixBytes(i + 1) + pixBytes(i + 2)) \ 3, Byte)
pixBytes(i) = temp
pixBytes(i + 1) = temp
pixBytes(i + 2) = temp
Next
Marshal.Copy(pixBytes, 0, tempPix, RowByteSize)

tempPix = IntPtr.op_Explicit(tempPix.ToInt32 + RowByteSize)
Count -= 1
Loop

UnLockBitmap()
End Sub
End Class

--
Jacob Grass
Microsoft .NET MVP
Check out http://windowsforms.net
posted on 2007-07-11 11:07  Thunderdanky  阅读(270)  评论(0)    收藏  举报