I think I have just had a revelation.

  • Thread starter Thread starter hexathioorthooxalate
  • Start date Start date
H

hexathioorthooxalate

If I were to create a user control, or to use any of the .Net stock
controls, then is it correct to say the properties and values shown in the
IDE properties window at design time for the control are ultimately
determined by reflection? The more I think about this the more I am
convincing myself that it has to be the case; feedback much appreciated.
Hexathioorthooxalate
 
The property grid gets all of its information through reflection. You can
also change the way the property grid sees your class by implementing
ICustomTypeDescriptor and feeding it bogus information. You can add
properties, change properties, remove properties and even rename them on-the
fly.

Cool stuff!

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com
 
Hi, Bob
I want to use your function Convert color images to grayscale
This is my code
I have got an error "Invalid paramter used" on this line
Dim c As Color = source.GetPixel(x, y) inside the function.

Can you help me?
Form Load

Dim bm As Bitmap = CType(Image.FromFile("D:\Documents and
Settings\UriD\Desktop\Ron\Ron1.jpg"), Bitmap)

'Me.PictureBox1.Image = bm

Me.PictureBox1.Image = ConvertToGrayscale(bm)

Me.PictureBox1.SizeMode = PictureBoxSizeMode.Normal

---------------

Public Function ConvertToGrayscale(ByVal source As Bitmap) As Bitmap

Dim bm As New Bitmap(source.Width, source.Height)

Dim x

Dim y

MsgBox(bm.Height)

MsgBox(bm.Width)

For y = 0 To bm.Height

For x = 0 To bm.Width

Dim c As Color = source.GetPixel(x, y)

Dim luma As Integer = CInt(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)

bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma))

Next

Next

Return bm

End Function
 
Hello,
In Visual Basic, the for loop includes the end value so your code is
stepping outside the bounds of the image.

Your code should read:

For y = 0 To bm.Height-1

For x = 0 To bm.Width-1

Dim c As Color = source.GetPixel(x, y)

Dim luma As Integer = CInt(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)

bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma))

Next

Next

However, this is not the best way to do it. That part of the article was
left in to illustrate the mechanics of the process rather than provide a
good solution. GetPixel and SetPixel is very slow.

The preferred method would be to copy the image using a ColorMatrix that
sets the gray scals for you such as:

protected function GetGrayImage(ByVal bm as Bitmap) as Bitmap
dim temp as new Bitmap(bm.Width, bm.Height)
dim ia as new ImageAttributes()
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}})
ia.SetColorMatrix(cm)
dim g as Graphics=Graphics.FromImage(temp)
g.Clear(Color.White)
g.DrawImage(bm,new Rectangle(0,0,temp.Width,temp.Height), 0, 0,
bm.Width, bm.Height, GraphicsUnit.Pixel, ia)
g.Dispose()
return temp
end function

Hope this helps.

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com
 
Back
Top