gorexy
asked on
Picture Binarization problem
Hi,
I would like to load a picture and convert into binary image
I can load the picture and convert but cannot save. what 's wrong?
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''
Private Sub cmdLoad_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLoad.Click
Dim hDC As Integer
CommonDialog1.Filter = "(*.bmp;*.ico;*.gif;*.jpg) |*.bmp;*.i co;*.gif;* .jpg"
CommonDialog1.ShowOpen()
sFile = CommonDialog1.FileName
If sFile <> "" Then
Picture1.Image = System.Drawing.Image.FromF ile(sFile)
'hDCSour = CreateCompatibleDC(Picture 1.Handle)
hDCSour = CreateCompatibleDC(hDC)
hBMPSour = CreateCompatibleBitmap(hDC , Picture1.ClientRectangle.W idth, Picture1.ClientRectangle.H eight)
hDCDest = CreateCompatibleDC(hDC)
hBMPDest = CreateCompatibleBitmap(hDC , Picture1.ClientRectangle.W idth, Picture1.ClientRectangle.H eight)
SelectObject(hDCSour, hBMPSour)
SelectObject(hDCDest, hBMPDest)
' BitBlt(hDCSour, 0, 0, Picture1.ClientRectangle.W idth, Picture1.ClientRectangle.H eight, hDC, 0, 0, vbSrcCopy)
BitBlt(hDCDest, 0, 0, Picture1.ClientRectangle.W idth, Picture1.ClientRectangle.H eight, hDCSour, 0, 0, SRCCOPY)
End If
End Sub
Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
'stores the counter value
Static a As Integer
a = a + 1
Picture1.Image.Save(VB6.Ge tPath & "\pict" & a & ".bmp")
End Sub
Sub Grayscaling(ByRef pfIndex As Short)
Dim pX, pY, hDC As Integer
Dim x, y As Integer
Dim colorval As Integer
Dim green, red, blue As Integer
Dim green2, red2, blue2 As Integer
pX = Picture1.ClientRectangle.W idth - 1
pY = Picture1.ClientRectangle.H eight - 1
'loop through each pixel
For x = 0 To pX
For y = 0 To pY
colorval = GetPixel(hDCSour, x, y)
If colorval >= 128 Then red2 = 255
If colorval < 128 Then red2 = 0
green2 = red2
blue2 = red2
SetPixel(hDCDest, x, y, RGB(red2, green2, blue2))
Next y
Next x
BitBlt(hDCDes, 0, 0, Picture1.ClientRectangle.W idth, Picture1.ClientRectangle.H eight, hDCSour, 0, 0, SRCCOPY)
Picture1.Refresh()
End Sub
Private Sub Form1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
DeleteObject(hBMPSour)
DeleteDC(hDCSour)
DeleteObject(hBMPDest)
DeleteDC(hDCDest)
End Sub
I would like to load a picture and convert into binary image
I can load the picture and convert but cannot save. what 's wrong?
''''''''''''''''''''''''''
Private Sub cmdLoad_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLoad.Click
Dim hDC As Integer
CommonDialog1.Filter = "(*.bmp;*.ico;*.gif;*.jpg)
CommonDialog1.ShowOpen()
sFile = CommonDialog1.FileName
If sFile <> "" Then
Picture1.Image = System.Drawing.Image.FromF
'hDCSour = CreateCompatibleDC(Picture
hDCSour = CreateCompatibleDC(hDC)
hBMPSour = CreateCompatibleBitmap(hDC
hDCDest = CreateCompatibleDC(hDC)
hBMPDest = CreateCompatibleBitmap(hDC
SelectObject(hDCSour, hBMPSour)
SelectObject(hDCDest, hBMPDest)
' BitBlt(hDCSour, 0, 0, Picture1.ClientRectangle.W
BitBlt(hDCDest, 0, 0, Picture1.ClientRectangle.W
End If
End Sub
Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
'stores the counter value
Static a As Integer
a = a + 1
Picture1.Image.Save(VB6.Ge
End Sub
Sub Grayscaling(ByRef pfIndex As Short)
Dim pX, pY, hDC As Integer
Dim x, y As Integer
Dim colorval As Integer
Dim green, red, blue As Integer
Dim green2, red2, blue2 As Integer
pX = Picture1.ClientRectangle.W
pY = Picture1.ClientRectangle.H
'loop through each pixel
For x = 0 To pX
For y = 0 To pY
colorval = GetPixel(hDCSour, x, y)
If colorval >= 128 Then red2 = 255
If colorval < 128 Then red2 = 0
green2 = red2
blue2 = red2
SetPixel(hDCDest, x, y, RGB(red2, green2, blue2))
Next y
Next x
BitBlt(hDCDes, 0, 0, Picture1.ClientRectangle.W
Picture1.Refresh()
End Sub
Private Sub Form1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
DeleteObject(hBMPSour)
DeleteDC(hDCSour)
DeleteObject(hBMPDest)
DeleteDC(hDCDest)
End Sub
ASKER
no errors just it cannot produce the BMP file
Mmmmm, the code in the cmdSave
'stores the counter value
Static a As Integer
a = a + 1
Picture1.Image.Save(VB6.Ge tPath & "\pict" & a & ".bmp")
a will always be 1, are you not simply overwriting the file (IS there a pict1.bmp file in the destination path?)
'stores the counter value
Static a As Integer
a = a + 1
Picture1.Image.Save(VB6.Ge
a will always be 1, are you not simply overwriting the file (IS there a pict1.bmp file in the destination path?)
Ooops my bad it was declared static (didn't see that)
This definitely looks like VB6 code ported over to VB.Net.
I haven't analyzed the code in depth...but I'm pretty sure you can achieve what you need using ALL native .Net code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "(*.bmp;*.ico;*.gif;*.jpg) |*.bmp;*.i co;*.gif;* .jpg"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult .OK Then
Dim fileName As String = OpenFileDialog1.FileName
Dim bmp As Bitmap = Bitmap.FromFile(fileName)
Dim bmp2 As New Bitmap(bmp)
Dim c As Color
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
c = bmp.GetPixel(x, y)
' analyze the color somehow...
If c.R < 125 And c.B < 125 Then
' set a pixel in your new bitmap...
bmp2.SetPixel(x, y, Color.FromArgb(someRedValu e, someGreenValue, someBlueValue))
End If
Next
Next
bmp.Dispose()
PictureBox1.Image = bmp2
bmp2.Save("yourNewFileName Here.bmp")
End If
End Sub
I haven't analyzed the code in depth...but I'm pretty sure you can achieve what you need using ALL native .Net code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "(*.bmp;*.ico;*.gif;*.jpg)
If OpenFileDialog1.ShowDialog
Dim fileName As String = OpenFileDialog1.FileName
Dim bmp As Bitmap = Bitmap.FromFile(fileName)
Dim bmp2 As New Bitmap(bmp)
Dim c As Color
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
c = bmp.GetPixel(x, y)
' analyze the color somehow...
If c.R < 125 And c.B < 125 Then
' set a pixel in your new bitmap...
bmp2.SetPixel(x, y, Color.FromArgb(someRedValu
End If
Next
Next
bmp.Dispose()
PictureBox1.Image = bmp2
bmp2.Save("yourNewFileName
End If
End Sub
Here is a working example of converting an image to grayscale:
http://www.bobpowell.net/grayscale.htm
Bob
http://www.bobpowell.net/grayscale.htm
Bob
ASKER
Hi Ide_mind
your code is great and simple, just a few questions:
1. what is C.B, C.R?
If i want to turn my pic into Black and White only, how to set?
2. it can output the bmp file but invalid (cannot be opened)
Thanks
your code is great and simple, just a few questions:
1. what is C.B, C.R?
If i want to turn my pic into Black and White only, how to set?
2. it can output the bmp file but invalid (cannot be opened)
Thanks
C.R, C.B and C.G will give you the value of the red, green and blue components of that color.
You can specify the image format when you save the file. Something like...
bmp2.Save(fileName, System.Drawing.Imaging.Ima geFormat.B mp)
Did you try the code in Bob's link?
Here is the formula in that link applied to my code. It saves a greyscale image using the same filename but with a .bmp extension. If the file was already a bmp then it is REPLACED with the greyscale version:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.Filter = "(*.bmp;*.ico;*.gif;*.jpg) |*.bmp;*.i co;*.gif;* .jpg"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult .OK Then
Dim fileName As String = OpenFileDialog1.FileName
Dim bmp As Bitmap = Bitmap.FromFile(fileName)
Dim bmp2 As New Bitmap(bmp)
bmp.Dispose()
Dim c As Color
Dim luma As Integer
For x As Integer = 0 To bmp2.Width - 1
For y As Integer = 0 To bmp2.Height - 1
c = bmp2.GetPixel(x, y)
luma = CInt(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)
bmp2.SetPixel(x, y, Color.FromArgb(luma, luma, luma))
Next
Next
Dim fi As New System.IO.FileInfo(fileNam e)
If fi.Extension.ToUpper <> ".BMP" Then
fileName = fi.FullName.Substring(0, fi.Name.LastIndexOf(fi.Ext ension)) & ".bmp"
End If
bmp2.Save(fileName, System.Drawing.Imaging.Ima geFormat.B mp)
End If
End Sub
You can specify the image format when you save the file. Something like...
bmp2.Save(fileName, System.Drawing.Imaging.Ima
Did you try the code in Bob's link?
Here is the formula in that link applied to my code. It saves a greyscale image using the same filename but with a .bmp extension. If the file was already a bmp then it is REPLACED with the greyscale version:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.Filter = "(*.bmp;*.ico;*.gif;*.jpg)
If OpenFileDialog1.ShowDialog
Dim fileName As String = OpenFileDialog1.FileName
Dim bmp As Bitmap = Bitmap.FromFile(fileName)
Dim bmp2 As New Bitmap(bmp)
bmp.Dispose()
Dim c As Color
Dim luma As Integer
For x As Integer = 0 To bmp2.Width - 1
For y As Integer = 0 To bmp2.Height - 1
c = bmp2.GetPixel(x, y)
luma = CInt(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)
bmp2.SetPixel(x, y, Color.FromArgb(luma, luma, luma))
Next
Next
Dim fi As New System.IO.FileInfo(fileNam
If fi.Extension.ToUpper <> ".BMP" Then
fileName = fi.FullName.Substring(0, fi.Name.LastIndexOf(fi.Ext
End If
bmp2.Save(fileName, System.Drawing.Imaging.Ima
End If
End Sub
ASKER
finally i use the code to display B/W pic
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
c = bmp.GetPixel(x, y)
' analyze the color somehow...
If c.R < 125 Then
' set a pixel in your new bitmap...
bmp2.SetPixel(x, y, Color.FromArgb(0, 0, 0))
End If
If c.B >= 125 Then
bmp2.SetPixel(x, y, Color.FromArgb(255, 255, 255))
End If
Next
Next
Can I further operate the thresholed pic?
For x As Integer = 0 To bmp.Width - 1
For y As Integer = 0 To bmp.Height - 1
c = bmp.GetPixel(x, y)
' analyze the color somehow...
If c.R < 125 Then
' set a pixel in your new bitmap...
bmp2.SetPixel(x, y, Color.FromArgb(0, 0, 0))
End If
If c.B >= 125 Then
bmp2.SetPixel(x, y, Color.FromArgb(255, 255, 255))
End If
Next
Next
Can I further operate the thresholed pic?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes the code works now.
as I am trying to play with barcode like reading and so I try to threhold the image into black/white and then decode them into 0101010...
do you think it works?
as I am trying to play with barcode like reading and so I try to threhold the image into black/white and then decode them into 0101010...
do you think it works?
Try changing
Picture1.Image.Save(VB6.Ge
to
Picture1.Image.Save(Applic
Also try placing a breakpoint to check that the path and filename are valid