Drawing a bmp using a MICR font -
09-13-2005
, 09:05 AM
I'm drawing an image using a MICR font and it draws fine w/no problem.
However I get all this noise in my bmp. I used some suggestions found at the
bobpowell.com (esp. onebit) but it basically strips out my bmp to look like a
font on the skinny.
Here is my code.
-------------------------------------
Public Shared Function CreateMICR(ByVal checkNbr As String) As Byte()
Dim pDrawing As New
System.Net.WebPermission(PermissionState.Unrestric ted)
pDrawing.Assert()
Try
Dim FullMICR As String = "C " & RTrim(checkNbr) & " C
A123456789A C1234567C"
Dim textLength As Integer = FullMICR.Length
Dim fontSize As Integer = 12
' Set canvas width & height
Dim width As Integer
Dim height As Integer
width = (fontSize * textLength) + 15
height = fontSize + 10
' Initialize graphics
Dim pic As System.Drawing.Bitmap = New
System.Drawing.Bitmap(width, height,
Drawing.Imaging.PixelFormat.Format32bppRgb)
Dim g As System.Drawing.Graphics =
System.Drawing.Graphics.FromImage(pic)
'' Set font style
Dim fontFmly As New FontFamily("MICR E13B")
Dim font As New Font(fontFmly, 12, FontStyle.Regular)
Dim f As New StringFormat
f.Alignment = StringAlignment.Center
f.LineAlignment = StringAlignment.Center
' Finally, draw the font
g.Clear(System.Drawing.Color.White)
g.DrawString(FullMICR, font, SystemBrushes.WindowText, 205, 13, f)
' Convert image to a byte stream
Dim imageBytes() As Byte = Nothing
Dim memoryStream As System.IO.MemoryStream = New
System.IO.MemoryStream
pic = ReduceColor(pic)
pic.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp)
' close everything
memoryStream.Flush()
memoryStream.Close()
pic.Dispose()
pic = Nothing
imageBytes = memoryStream.ToArray()
memoryStream.Close()
Return (imageBytes)
Catch ex As Exception
Console.WriteLine(ex.Message())
End Try
End Function
Private Shared Sub SetIndexedPixel(ByVal x As Integer, ByVal y As
Integer, ByVal bmd As BitmapData, ByVal pixel As Boolean)
Dim index As Integer = y * bmd.Stride + (x >> 3)
Dim p As Byte = Marshal.ReadByte(bmd.Scan0, index)
Dim mask As Byte = CByte(&H80 >> (x And &H7))
If pixel Then
p = p Or mask
Else
p = p And CByte(mask ^ &HFF)
End If
Marshal.WriteByte(bmd.Scan0, index, p)
End Sub 'SetIndexedPixel
Public Shared Function ReduceColor(ByVal Img As Bitmap) As Bitmap
Dim bmdo As BitmapData = Img.LockBits(New Rectangle(0, 0, Img.Width,
Img.Height), ImageLockMode.ReadOnly, Img.PixelFormat)
'and the new 1bpp bitmap
Dim bm As Bitmap
bm = New Bitmap(Img.Width, Img.Height, PixelFormat.Format1bppIndexed)
bm.SetResolution(Img.HorizontalResolution, Img.VerticalResolution)
Dim bmdn As BitmapData = bm.LockBits(New Rectangle(0, 0, bm.Width,
bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed)
'scan through the pixels Y by X
Dim y As Integer
For y = 0 To Img.Height - 1
Dim x As Integer
For x = 0 To Img.Width - 1
'generate the address of the colour pixel
Dim index As Integer = y * bmdo.Stride + x * 4
'check its brightness
If Color.FromArgb(Marshal.ReadByte(bmdo.Scan0, index + 2),
Marshal.ReadByte(bmdo.Scan0, index + 1), Marshal.ReadByte(bmdo.Scan0,
index)).GetBrightness() > 0.5F Then
SetIndexedPixel(x, y, bmdn, True) 'set it if its bright.
End If
Next x
Next y
'tidy up
bm.UnlockBits(bmdn)
Img.UnlockBits(bmdo)
Img.Dispose()
Img = Nothing
Return bm
End Function
----------end code ----------------------
any idea why my font would get stripped? |