|
-
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
-
Aug 29th, 2008, 11:02 PM
#2
Fanatic Member
Re: [2005] Graphics.DrawImage incorrectly scaling an image?
I ran your code. I pretty much got the results you were talking about.
There's a line that I commented out
Code:
Dim a As New BarcodeLib.Barcode
a wasn't being used in that sub.
Where you have
Code:
e.Graphics.DrawImage(bc, New Rectangle(Point.Empty, Label1.Size))
I tried experimenting with rectangle and got rid of Point.Empty, Label1.Size.
I came up with this instead.
Code:
Dim w, h As Integer
h = Label1.Height
w = Label1.Width
e.Graphics.DrawImage(bc, New Rectangle(0, 0, w, 2 * h))
I'm running Visual Basic 2008 not 2005. What's weird is that I had to multiply height of label by 2 to get the barcode to display taller. Don't know why.
I attached screenshots. First using your code and second using my code.
Last edited by EntityX; Mar 28th, 2010 at 04:02 PM.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
-
Aug 30th, 2008, 12:21 AM
#3
Thread Starter
Fanatic Member
Re: [2005] Graphics.DrawImage incorrectly scaling an image?
whoops, forgot to comment that out myself. the Barcode reference isn't relevant to this problem, hence the other commented out code.
I figured I could just scale it to twice the height, but I'm curious as to why it's doing this in the first place.
-
Aug 30th, 2008, 12:33 AM
#4
Re: [2005] Graphics.DrawImage incorrectly scaling an image?
Have you tested similar things with other images?
Also, this is an odd way to display a barcode. They are usually done by displaying text in a specific font.
-
Aug 30th, 2008, 12:55 AM
#5
Thread Starter
Fanatic Member
Re: [2005] Graphics.DrawImage incorrectly scaling an image?
 Originally Posted by jmcilhinney
...Also, this is an odd way to display a barcode. They are usually done by displaying text in a specific font.
Actually, this is more common than you think, especially in codes where there is not a 1:1 ratio of segments (combination of bars) to digits. Examples include upc-a (half the code is black-white inverted from the other half) and pharmacode (the bars are a binary representation of a single number).
I need absolute control over the size of the code and may need to stretch it vertically. Unlike regular text, 1-dimensional barcodes don't have to maintain an aspect ratio to be readable. This is why I start with a bitmap one pixel in height. It's easier to set individual pixels than to draw a bunch of boxes (bars).
Not to mention, I have zero budget for this project. I can't purchase a font or five to make this work.
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
|