Turning a picture to white and black only

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
14 posts Page 1 of 2
Contributors
User avatar
skyteam
Member
Member
Posts: 27
Joined: Thu Apr 28, 2011 1:27 am

Hi guys how would I would I go about this I want to change from colored to white and black hehaho;
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

hello sky

this will do the job

i hope you mean grayscale?
Code: Select all
Public Function grayscale(ByVal img As Image) As Boolean
        Dim cm As New System.Drawing.Imaging.ColorMatrix(New Single()() _
                               {New Single() {0.299, 0.299, 0.299, 0, 0}, _
                                New Single() {0.587, 0.587, 0.587, 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 drawImage(img, cm)
    End Function
use it like this:
Code: Select all
grayscale(PictureBox1.Image)
        PictureBox1.Invalidate()
Dummy1912
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

try this black and white
Code: Select all
Imports System.Drawing
 Imports System.Drawing.Imaging
 Dim bitmap As Bitmap
 bitmap = New Bitmap( Me.Picture1.Image)
 Dim x, y
 Dim d As Byte
 For x = 0 To Me.Picture.Image.Width - 1
 For y = 0 To Me.Picture.Image.Height - 1
 d = Math.Round((bitmap.GetPixel(x, y).R * 0.299 + bitmap.GetPixel(x, y).G * 0.587 + bitmap.GetPixel(x, y).B * 0.114))
 bitmap.SetPixel(x, y, Color.FromArgb(d, d, d))
 Next y
 Next x
 Me.Picture1.Image = bitmap
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
skyteam
Member
Member
Posts: 27
Joined: Thu Apr 28, 2011 1:27 am

hey i get an error on drawimage(img, cm) 'drawimage' is not declared. It may be inaccessible due to its protection level.

tryed to declare it and still gives me error
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

did you import this

Imports System.Drawing
Imports System.Drawing.Imaging
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
skyteam
Member
Member
Posts: 27
Joined: Thu Apr 28, 2011 1:27 am

Nvm thanks got it to work
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

hmmm

is drawimage a picturebox?
so you need to do:
Code: Select all
drawimage.image = openfiledialog1.filename
but it must work
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Code: Select all
Public Sub BlackWhite(ByVal img As Bitmap)
   Dim bmd As BitmapData = img.LockBits(New Rectangle(Point.Empty, img.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
   Dim data(0 To bmd.Stride * bmd.Height) As Byte
   Marshal.Copy(bmd.Scan0, data, 0, data.Length)

   Dim r, g, b As Byte

   For y = 0 To img.Height
      For x = 0 To img.Width
          b = data(y * bmd.Stride + x * 3)
          g = data(y * bmd.Stride + x * 3 + 1)
          r = data(y * bmd.Stride + x * 3 + 2)

          If b + g + r > 381 Then
             data(y * bmd.Stride + x * 3) = 255
             data(y * bmd.Stride + x * 3 + 1) = 255
             data(y * bmd.Stride + x * 3 + 2) = 255
          Else
             data(y * bmd.Stride + x * 3) = 0
             data(y * bmd.Stride + x * 3 + 1) = 0
             data(y * bmd.Stride + x * 3 + 2) = 0
          End If
      Next
   Next

   Marshal.Copy(data, bmd.Scan0, 0, data.Length)
   img.UnlockBits(bmd)
End Sub
I guess that will work
EDIT!!
I made an awful mistake, I swapped height and width. Now it will work
Last edited by MrAksel on Sun Jan 29, 2012 9:14 pm, edited 1 time in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
skyteam
Member
Member
Posts: 27
Joined: Thu Apr 28, 2011 1:27 am

Yup it works but its it turns red into gray and my OCR cant recognize it I would need the black to turn to white and the red to turn to white and the white to turn to black since some text is white oh and green to white to

Image
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

You can change the value 381 to something lower if you want more to turn white. Or make it higher if more is to turend black.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
14 posts Page 1 of 2
Return to “Tutorial Requests”