-
Apr 21st, 2006, 01:57 PM
#1
Thread Starter
Lively Member
[2005] color --> black & white
How do you convert an image in an imagebox that is in color, to black and white and/or get the edges inside the image with Visual Studio 2005?
-
Apr 21st, 2006, 04:01 PM
#2
Fanatic Member
Re: [2005] color --> black & white
A very simple solution would be to have two copies of the image, one in color, and one in grayscale. Then use whatever event (ie button click) to 'convert' the image, but just replace it with the black and white one.
VB Code:
PictureBox1.Image = "GrayScaleImage.bmp"
In the unlikely event that I answer your question correctly, please Rate my post
Using Visual Studio 2005 Professional
-
Apr 21st, 2006, 04:03 PM
#3
Thread Starter
Lively Member
Re: [2005] color --> black & white
yeah but how can I convert it?
-
Apr 21st, 2006, 04:40 PM
#4
Addicted Member
Re: [2005] color --> black & white
there might be an easier way...but...in express 2005
acquire the image and pass it to your function
edit the numbers (ie .299 .114) to achieve desired effect.
VB Code:
Public Function grayscale1(ByVal img As Image, ByVal Instance As Integer) As Boolean
Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {500, 500, 500, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Return draw_adjusted_image(img, cm, Instance)
draw and save it...
VB Code:
rivate Function draw_adjusted_image(ByVal img As Image, ByVal cm As Imaging.ColorMatrix, _
ByVal instance As Integer) As Boolean
Try
Dim bmp As New Bitmap(img) ' create a copy of the source image
Dim imgattr As New Imaging.ImageAttributes()
Dim rc As New Rectangle(0, 0, img.Width, img.Height)
Dim g As Graphics = Graphics.FromImage(img)
' associate the ColorMatrix object with an ImageAttributes object
imgattr.SetColorMatrix(cm)
' draw the copy of the source image back over the original image,
'applying the ColorMatrix
g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, _
GraphicsUnit.Pixel, imgattr)
Dim strPath As String = Application.StartupPath ' & "\image"
img.Save("image.jpg")
g.Dispose()
Return True
Catch
'code....
End Try
End Function
and then you can code the load into your picture box.
Dreaming men are haunted men.
-
Apr 21st, 2006, 04:59 PM
#5
Re: [2005] color --> black & white
Check out this thread where it had been discussed before...
http://www.vbforums.com/showthread.php?t=376900
-
Apr 22nd, 2006, 07:43 AM
#6
Thread Starter
Lively Member
Re: [2005] color --> black & white
I came out on this, but it isn't working :|
I call the bw() inside the main load.
VB Code:
Public Function grayscale1(ByVal img As Drawing.Image, ByVal Instance As Integer) As Boolean
Dim cm As Drawing.Imaging.ColorMatrix = New Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {500, 500, 500, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Return draw_adjusted_image(img, cm, Instance)
End Function
Private Function draw_adjusted_image(ByVal img As Drawing.Image, ByVal cm As Drawing.Imaging.ColorMatrix, _
ByVal instance As Integer) As Boolean
Try
Dim bmp As New Drawing.Bitmap(img) ' create a copy of the source image
Dim imgattr As New Drawing.Imaging.ImageAttributes()
Dim rc As New Drawing.Rectangle(0, 0, img.Width, img.Height)
Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(img)
' associate the ColorMatrix object with an ImageAttributes object
imgattr.SetColorMatrix(cm)
' draw the copy of the source image back over the original image,
'applying the ColorMatrix
g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, _
Drawing.GraphicsUnit.Pixel, imgattr)
Dim strPath As String = Application.StartupPath ' & "\image"
img.Save("\image.bmp")
g.Dispose()
Return True
Catch
'code....
End Try
codebox2.ImageLocation = "\image.bmp"
End Function
Public Function bw()
Dim image As Drawing.Bitmap
image = PictureBox1.Image
grayscale1(image, True)
End Function
-
Apr 22nd, 2006, 08:25 AM
#7
Thread Starter
Lively Member
Re: [2005] color --> black & white
Never mind got it already
just forgotten something
-
Apr 22nd, 2006, 08:47 AM
#8
Thread Starter
Lively Member
Re: [2005] color --> black & white
Hmmm, ive another prob.
if the background of the image is white, then there's an whole white new grey-scale image created. and if the background is black, then he just does fine.
So I want to make an check if the background is white or black at some x/y coordinates. how to get a pixel at postion x, y ?
and this code converts color into white and leaves black as it is:
VB Code:
Dim cm As Drawing.Imaging.ColorMatrix = New Drawing.Imaging.ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {500, 500, 500, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
but how to leave white as it is and converts color into black?
Last edited by Wytse Talsma; Apr 22nd, 2006 at 08:57 AM.
-
Apr 22nd, 2006, 01:02 PM
#9
Addicted Member
Re: [2005] color --> black & white
try
VB Code:
Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {0.599, 0.599, 0.599, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
if that doesn't work for you, try adjusting the numbers
Dreaming men are haunted men.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|