|
-
Aug 29th, 2008, 08:43 PM
#1
Thread Starter
Fanatic Member
[2005] Graphics.DrawImage incorrectly scaling an image?
I'm writing some code that draws a barcode at a given size and scales it. It starts off with an image that varies in width but is always one pixel in height.
When I'm ready to draw the barcode, it scales the image to fit into a label control (a simple call to e.Graphics.DrawImage(...) in the Paint event).
What I'm getting, however is a barcode that is half the height of the label control, regardless of the size of the label control.
To use this, create a new form with a text box and a label. Label.autosize = false, text="". Anchor all four sides. Once running, type something into the text box. The code converts the 1 and 0 into black and white pixels respectively and sets a private field to the new bitmap. The label draws from this bitmap. Any ideas why it only fills half the label even though I provided label1.size to draw image? Seems like a bug.
vb Code:
Public Class Form1
Private bc As Bitmap
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If Not TextBox1.Text = "" Then
Dim a As New BarcodeLib.Barcode
'a.RawData = TextBox1.Text
'a.EncodedType = BarcodeLib.TYPE.CODE128
'a.Encode()
'bc = BCFromEncodedData(a.EncodedValue)
bc = BCFromEncodedData("1011001101100110110011011001101100110110011011001")
Label1.Refresh()
Else
bc = Nothing
Label1.Refresh()
End If
End Sub
Private Function BCFromEncodedData(ByVal data As String) As Bitmap
Dim a As New Bitmap(data.Length, 1, Imaging.PixelFormat.Format32bppArgb)
For i As Integer = 0 To data.Length - 1
If data(i) = "1" Then a.SetPixel(i, 0, Color.Black)
Next
Return a
End Function
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
If bc IsNot Nothing Then
e.Graphics.DrawImage(bc, New Rectangle(Point.Empty, Label1.Size))
End If
End Sub
Private Sub Label1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.SizeChanged
Refresh()
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|