|
-
May 8th, 2002, 09:35 AM
#1
Thread Starter
Member
Bug with StretchImage in PictureBox?
StrechImage SizeMode does not work as expected when I assign a code-generated gradient Bitmap to a PictureBox. The StrechImage function seems to add a row (or column) of gray pixels to the Bitmap before the Bitmap is stretched. To see what I'm talking about, do the following:
1. Create a new WindowsForm Project
2. In design mode, add a Button, a TextBox, and a PictureBox (keeping the original names for the controls)
3. Set the SizeMode property of the PictureBox to StretchImage
4. Copy and paste the following code to the Form1 class
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.Text = 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, j As Integer
Dim c As Drawing.Color
Dim c_Blue_Temp As Integer
Dim Bmp As Drawing.Bitmap
Const Bmp_Wid As Integer = 256 'Make sure that Bmp_Wid is not greater than 256!
Dim Bmp_Ht As Integer = CInt(Me.TextBox1.Text)
'Force the Width of PictureBox1 to be Bmp_Wid pixels wide so that there won't be any stretching in the x-direction
Me.PictureBox1.Width = Bmp_Wid
'Create a Bitmap
Bmp = New Drawing.Bitmap(Bmp_Wid, Bmp_Ht, Drawing.Imaging.PixelFormat.Format32bppArgb)
'Fill the Bitmap with a Gradient
For i = 0 To Bmp_Wid - 1
c_Blue_Temp = i
c = c.FromArgb(255, 0, 0, c_Blue_Temp)
For j = 0 To Bmp.Height - 1
Bmp.SetPixel(i, j, c)
Next j
Next i
'Assign the Bitmap to PictureBox1
Me.PictureBox1.Image = Bmp
End Sub
5. Run the program and click the Button
Notice that the colors in the lower half of PictureBox's image seem to be obtained by interpolating between the colors in our Bitmap and Gray. In other words, it is as if a row of gray pixels was added to our Bitmap BEFORE the image was stretched.
Now, increment the value in the TextBox and press the Button after each increment. That phantom row of gray pixels is always present until the value in the TextBox equals or exceeds the height of the PictureBox.
So why do I care? Well, I'm glad you asked. Stretched 1D Bitmaps update MUCH faster in a PictureBox than 2D Bitmaps that completely fill a PictureBox. Of course, creating a 1D Bitmap is also much faster than creating a 2D Bitmap. So, the above method is the best way that I've found for creating a permanent gradient image.
A work around for this problem is to create a PictureBox inside of a Container. The, you could just make the height of the PictureBox larger than the container so that the interpolated gray region was not visible. But, lets face it, there must be a better way.
Any ideas? Thanks.
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
|