Results 1 to 9 of 9

Thread: [2005] color --> black & white

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    110

    [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?

  2. #2
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    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:
    1. PictureBox1.Image = "GrayScaleImage.bmp"

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    110

    Re: [2005] color --> black & white

    yeah but how can I convert it?

  4. #4
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    Re: [2005] color --> black & white

    there might be an easier way...but...in express 2005


    acquire the image and pass it to your function
    VB Code:
    1. grayscale1(image)


    edit the numbers (ie .299 .114) to achieve desired effect.

    VB Code:
    1. Public Function grayscale1(ByVal img As Image, ByVal Instance As Integer) As Boolean
    2.  
    3.         Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
    4.                            {New Single() {0.299, 0.299, 0.299, 0, 0}, _
    5.                             New Single() {500, 500, 500, 0, 0}, _
    6.                             New Single() {0.114, 0.114, 0.114, 0, 0}, _
    7.                             New Single() {0, 0, 0, 1, 0}, _
    8.                             New Single() {0, 0, 0, 0, 1}})
    9.  
    10.  
    11.         Return draw_adjusted_image(img, cm, Instance)

    draw and save it...
    VB Code:
    1. rivate Function draw_adjusted_image(ByVal img As Image, ByVal cm As Imaging.ColorMatrix, _
    2.                                             ByVal instance As Integer) As Boolean
    3.  
    4.         Try
    5.             Dim bmp As New Bitmap(img) ' create a copy of the source image
    6.             Dim imgattr As New Imaging.ImageAttributes()
    7.             Dim rc As New Rectangle(0, 0, img.Width, img.Height)
    8.             Dim g As Graphics = Graphics.FromImage(img)
    9.  
    10.             ' associate the ColorMatrix object with an ImageAttributes object
    11.             imgattr.SetColorMatrix(cm)
    12.  
    13.             ' draw the copy of the source image back over the original image,
    14.             'applying the ColorMatrix
    15.             g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, _
    16.                                    GraphicsUnit.Pixel, imgattr)
    17.  
    18.             Dim strPath As String = Application.StartupPath ' & "\image"
    19.             img.Save("image.jpg")
    20.  
    21.  
    22.             g.Dispose()
    23.  
    24.             Return True
    25.  
    26.         Catch
    27.             'code....
    28.         End Try
    29.  
    30.     End Function
    and then you can code the load into your picture box.
    Dreaming men are haunted men.

  5. #5
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] color --> black & white

    Check out this thread where it had been discussed before...

    http://www.vbforums.com/showthread.php?t=376900

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    110

    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:
    1. Public Function grayscale1(ByVal img As Drawing.Image, ByVal Instance As Integer) As Boolean
    2.  
    3.         Dim cm As Drawing.Imaging.ColorMatrix = New Drawing.Imaging.ColorMatrix(New Single()() _
    4.                            {New Single() {0.299, 0.299, 0.299, 0, 0}, _
    5.                             New Single() {500, 500, 500, 0, 0}, _
    6.                             New Single() {0.114, 0.114, 0.114, 0, 0}, _
    7.                             New Single() {0, 0, 0, 1, 0}, _
    8.                             New Single() {0, 0, 0, 0, 1}})
    9.  
    10.  
    11.         Return draw_adjusted_image(img, cm, Instance)
    12.  
    13.     End Function
    14.     Private Function draw_adjusted_image(ByVal img As Drawing.Image, ByVal cm As Drawing.Imaging.ColorMatrix, _
    15.                                             ByVal instance As Integer) As Boolean
    16.  
    17.         Try
    18.             Dim bmp As New Drawing.Bitmap(img) ' create a copy of the source image
    19.             Dim imgattr As New Drawing.Imaging.ImageAttributes()
    20.             Dim rc As New Drawing.Rectangle(0, 0, img.Width, img.Height)
    21.             Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(img)
    22.  
    23.             ' associate the ColorMatrix object with an ImageAttributes object
    24.             imgattr.SetColorMatrix(cm)
    25.  
    26.             ' draw the copy of the source image back over the original image,
    27.             'applying the ColorMatrix
    28.             g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, _
    29.                                    Drawing.GraphicsUnit.Pixel, imgattr)
    30.  
    31.             Dim strPath As String = Application.StartupPath ' & "\image"
    32.             img.Save("\image.bmp")
    33.  
    34.  
    35.             g.Dispose()
    36.  
    37.             Return True
    38.  
    39.         Catch
    40.             'code....
    41.         End Try
    42.         codebox2.ImageLocation = "\image.bmp"
    43.     End Function
    44.  
    45.     Public Function bw()
    46.         Dim image As Drawing.Bitmap
    47.         image = PictureBox1.Image
    48.         grayscale1(image, True)
    49.     End Function

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    110

    Re: [2005] color --> black & white

    Never mind got it already
    just forgotten something

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    110

    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:
    1. Dim cm As Drawing.Imaging.ColorMatrix = New Drawing.Imaging.ColorMatrix(New Single()() _
    2.                                {New Single() {0.299, 0.299, 0.299, 0, 0}, _
    3.                                 New Single() {500, 500, 500, 0, 0}, _
    4.                                 New Single() {0.114, 0.114, 0.114, 0, 0}, _
    5.                                 New Single() {0, 0, 0, 1, 0}, _
    6.                                 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.

  9. #9
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    Re: [2005] color --> black & white

    try

    VB Code:
    1. Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
    2.                            {New Single() {0.299, 0.299, 0.299, 0, 0}, _
    3.                             New Single() {0.599, 0.599, 0.599, 0, 0}, _
    4.                             New Single() {0.114, 0.114, 0.114, 0, 0}, _
    5.                             New Single() {0, 0, 0, 1, 0}, _
    6.                             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
  •  



Click Here to Expand Forum to Full Width