You are on page 1of 2

'************************************** ' Name: Image to Binary Converter ' Description:This code converts a 240(wide) x 128(tall) pixels monochrome pictu

re into a text file that displays the image in ones and zeroes. Useful for uploa ding monochrome pictures out the com port to a device that uses an LCD display. ' By: Oliver French ' ' Inputs:Any monochrome file. If it is not 240 x 128 pixels, then the image will be skewed to these dimensions. Some resolution may be lost if skewing occurs. ' ' Returns:A text file with the image in binary. ' ' Assumes:Your form needs to have A BorderStyle = 0 or the image will be scanned incorrectly. You need 3 buttons named cmdLoad, cmdExit, & cmdSendData. You also need 2 dialog controls named dlgSaveData and dlgLoadPic. Finally you need an im age named imgData. Forgot to mention - set the scalmode of your form to 3 (pixel s). This lets you find the point where you want to start scanning. ' 'This code is copyrighted and has' limited warranties.Please see http://www.Plan et-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=22944&lngWId=1'for details. '************************************** Private Sub cmdExit_Click() End End Sub Private Sub cmdLoad_Click() Dim sFile As String With dlgLoadPic .DialogTitle = "Open" .CancelError = False .Filter = "Bitmap Files (*.BMP)|*.BMP" .ShowOpen If Len(.FileName) = 0 Then Exit Sub End If sFile = .FileName End With imgData.Picture = LoadPicture(sFile) End Sub Private Sub cmdSendData_Click() Dim x As Long Dim y As Long Dim sTmp As String Dim lColor As Long Dim lDC As Long Dim lngTop As Long Dim lngLeft As Long Dim lngPicDat(240, 128) As Long Dim g As Integer Dim b As Integer Dim intDat As Long Dim strPicLine As String Dim strPicRows(128) As String Dim sFile As String Dim lngFormTop As Long Dim lngFormLeft As Long

' Determine image Top and Left pixel positions lngTop = (Me.Top / Screen.TwipsPerPixelY) + imgData.Top lngLeft = (Me.Left / Screen.TwipsPerPixelX) + imgData.Left lDC = GetWindowDC(0) For g = 1 To 128 ' Loop through every row For b = 1 To 240 ' Loop through every column intDat = GetPixel(lDC, b + lngLeft, g + lngTop) ' Get pixel color of coordinate If intDat = 0 Then ' Pixel is black lngPicDat(b, g) = 1 End If If intDat = 16777215 Then ' Pixel is white lngPicDat(b, g) = 0 End If strPicLine = Trim(strPicLine + Trim(Str(lngPicDat(b, g)))) ' Add the bit to the line string Next b strPicRows(g) = strPicLine ' Save each completed row in an array strPicLine = "" Next g ' Output the data to a text file With dlgSaveData .DialogTitle = "Save" .CancelError = False .Filter = "Text Files (*.Txt)|*.txt" .ShowSave If Len(.FileName) = 0 Then Exit Sub End If sFile = .FileName End With Open sFile For Append As #1 For g = 1 To 128 Print #1, strPicRows(g) Next g Close #1 MsgBox "Done!" End Sub Private Sub Form_Load() If Me.BorderStyle <> 0 Then MsgBox "You need to set the borderstyle of your for m to 0" ' Center the form Me.Left = (Screen.Width - Me.Width) \ 2 Me.Top = (Screen.Height - Me.Height) \ 2 imgData.Width = 240 imgData.Height = 128 End Sub

You might also like