Results 1 to 9 of 9

Thread: drawing straight to an in-code bitmap?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    drawing straight to an in-code bitmap?

    I can't seem to figure out how to draw a gradient to a bitmap I created in code. I have the background fading with a crazy bouncing sort of gradient with this code (paste it into a project if U wanna see it )
    VB Code:
    1. Dim m_intAntiBlueC1 As Integer = 255
    2.     Dim m_intAntiBlueC2 As Integer = 0
    3.     Dim m_blnC1GoUp As Boolean
    4.  
    5. Dim graphics As Graphics
    6.         graphics = Me.CreateGraphics()
    7.         Dim c1 As Color = Color.FromArgb(m_intAntiBlueC1, m_intAntiBlueC1, 200)
    8.         Dim c2 As Color = Color.FromArgb(m_intAntiBlueC2, m_intAntiBlueC2, 200)
    9.         Dim brush As New Drawing2D.LinearGradientBrush(Me.DisplayRectangle, c1, c2, Drawing2D.LinearGradientMode.ForwardDiagonal)
    10.         graphics.FillRectangle(brush, Me.DisplayRectangle)
    11. [COLOR=Red]        Dim TheBit As New Bitmap(295, 80)
    12.         Dim graphics2 As Graphics[/COLOR]
    13.  
    14.         If m_intAntiBlueC1 = 255 Then
    15.             m_blnC1GoUp = False
    16.         ElseIf m_intAntiBlueC1 = 0 Then
    17.             m_blnC1GoUp = True
    18.         End If
    19.  
    20.         If m_blnC1GoUp Then
    21.             m_intAntiBlueC1 += 1
    22.             m_intAntiBlueC2 -= 1
    23.         Else
    24.             m_intAntiBlueC1 -= 1
    25.             m_intAntiBlueC2 += 1
    26.         End If
    I can't seem to create a graphics area from the bitmap itself so I don't know any other way to draw graphics data onto the bitmap. Anyone know how to do it?
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: drawing straight to an in-code bitmap?

    graphics = Me.CreateGraphics() creates a graphics object for the canvas of the object specified (Me, aka the form)

    Bitmaps don't have a canvas to draw on.. they are simply a collection of bytes that make up the image.

    You could load the bitmap into a picturebox, draw on the picturebox canvas, and then save the picturebox to file...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: drawing straight to an in-code bitmap?

    I just added some code through the wonderful logic of creating a new object every time it asked for one without fully knowing what the object is :P
    VB Code:
    1. Dim TheBit As New Bitmap(295, 80)
    2.         Dim graphics2 As Graphics
    3.         Dim IntPtr As IntPtr
    4.         IntPtr = TheBit.GetHbitmap()
    5.         graphics2 = Drawing.Graphics.FromHwnd(IntPtr)
    6.         Dim rect As New RectangleF()
    7.         rect = graphics2.VisibleClipBounds
    8.         graphics2.FillRectangle(brush, rect)
    but it stops at the graphics2 = Drawing.Graphics.FromHwnd(IntPtr) line and gives me an error that simply says "out of memory" and that's it. I'm gonna ask my teacher tonight to teach us about handles and pointers and all that crap for like the millionth time grrrrrrrrrr
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: drawing straight to an in-code bitmap?

    yeah I know doing it with a picturebox would be super simple but I eventually want to assign the bitmap to the background image on a label control, effectively making a really cool moving background. I think I'll just stick a picturebox under the label and make it transparent
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  5. #5
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: drawing straight to an in-code bitmap?

    this is one of the ways to do it
    VB Code:
    1. Dim bmp As New Bitmap(Width, Height)
    2.         Dim g As Graphics
    3.         g = Graphics.FromImage(bmp)
    4.  
    5.         'use the g to draw any thing
    6.  
    7.         'dispose the graphics object.
    8.         g.Dispose()

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: drawing straight to an in-code bitmap?

    well, that didn't work. I set the backcolor to transparent and it still shows up as control gray or whatever. That's never happened before, what's up with that?
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: drawing straight to an in-code bitmap?

    I am not sure what you are doing but using your code i get this.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim bmp As New Bitmap(295, 80)
    3.         Dim g As Graphics
    4.         g = Drawing.Graphics.FromImage(bmp)
    5.  
    6.         Dim m_intAntiBlueC1 As Integer = 255
    7.         Dim m_intAntiBlueC2 As Integer = 0
    8.         Dim m_blnC1GoUp As Boolean
    9.         Dim c1 As Color = Color.FromArgb(m_intAntiBlueC1, m_intAntiBlueC1, 200)
    10.         Dim c2 As Color = Color.FromArgb(m_intAntiBlueC2, m_intAntiBlueC2, 200)
    11.         Dim brush As New Drawing2D.LinearGradientBrush(Me.DisplayRectangle, c1, c2, Drawing2D.LinearGradientMode.ForwardDiagonal)
    12.         g.FillRectangle(brush, Me.DisplayRectangle)
    13.        
    14.         If m_intAntiBlueC1 = 255 Then
    15.             m_blnC1GoUp = False
    16.         ElseIf m_intAntiBlueC1 = 0 Then
    17.             m_blnC1GoUp = True
    18.         End If
    19.  
    20.         If m_blnC1GoUp Then
    21.             m_intAntiBlueC1 += 1
    22.             m_intAntiBlueC2 -= 1
    23.         Else
    24.             m_intAntiBlueC1 -= 1
    25.             m_intAntiBlueC2 += 1
    26.         End If
    27.         brush.Dispose()
    28.         g.Dispose()
    29.         Me.PictureBox1.Image = bmp
    30.  
    31.     End Sub
    Attached Images Attached Images  

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: drawing straight to an in-code bitmap?

    uh oh, post delay :P that one above my last one worked w00t. Why did they have to hide FromImage so well? geeeeeeeeeeze. But for some odd reason when I do TheLabel.Image = TheBitmap, it renders correctly on the left but not on the right. It's like it's overflowing even though I double checked and the label and bitmap and graphics area are all drawing to the correct size. I'll post a video or gif of it when I get home :P
    Okay nm here's screenshots :P here's the important parts of code having to do with size:
    VB Code:
    1. Dim TheBit As New Bitmap(295, 80)
    2. graphics2 = Drawing.Graphics.FromImage(TheBit)
    3. graphics2.FillRectangle(brush, 0, 0, 295, 80)
    and yet, here's one "peak" and the other "peak" of the gradient cycle from 0-255. According to Pixie, it only reaches about 33 on one peak and 86 on the other, suggesting the gradient bitmap doesn't fit in the area, except the label really is 295x80. Like on the 2nd peak here, it should be 100% purple on the right and on the first one it should be close to 100% yellow on the left but they're WAY off



    Here's the actual, complete code if U wanna try it:
    VB Code:
    1. 'module lvl:
    2.     Dim m_intAntiBlueC1 As Integer = 255
    3.  
    4. 'timer tick sub:
    5.         If m_intAntiBlueC1 >= 255 Then
    6.             m_blnC1GoUp = False
    7.         ElseIf m_intAntiBlueC1 <= 0 Then
    8.             m_blnC1GoUp = True
    9.         End If
    10.  
    11.         If m_blnC1GoUp Then
    12.             m_intAntiBlueC1 += 3
    13.  
    14.         Else
    15.             m_intAntiBlueC1 -= 3
    16.  
    17.         End If
    18.  
    19.         Dim graphics As Graphics
    20.         graphics = Me.CreateGraphics()
    21.         Dim c1 As Color = Color.FromArgb(m_intAntiBlueC1, m_intAntiBlueC1, 200)
    22.         Dim c2 As Color = Color.FromArgb(255 - m_intAntiBlueC1, 255 - m_intAntiBlueC1, 200)
    23.         Dim brush As New Drawing2D.LinearGradientBrush(Me.DisplayRectangle, c1, c2, Drawing2D.LinearGradientMode.ForwardDiagonal)
    24.         graphics.FillRectangle(brush, Me.DisplayRectangle)
    25.         Dim TheBit As New Bitmap(295, 80)
    26.         Dim graphics2 As Graphics
    27.         graphics2 = Drawing.Graphics.FromImage(TheBit)
    28.  
    29.  
    30.         c1 = Color.FromArgb(255, m_intAntiBlueC1, 200)
    31.         c2 = Color.FromArgb(255, 255 - m_intAntiBlueC1, 200)
    32.         brush = New Drawing2D.LinearGradientBrush(Me.DisplayRectangle, c1, c2, Drawing2D.LinearGradientMode.Horizontal)
    33.         graphics2.FillRectangle(brush, 0, 0, 295, 80)
    34.         lblLogo.Image = TheBit
    Last edited by Desolator144; Nov 2nd, 2006 at 07:42 PM.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: drawing straight to an in-code bitmap?

    OMG
    I just realized I was using the form's display rectangle in the redeclaration of the brush! AHHHHHHHHHHHH!!!!!!!!!!!!!!!
    Here's the real real real real real REAL code if U wanna see the absolute beautiful awesome!
    VB Code:
    1. If m_intAntiBlueC1 >= 255 Then
    2.             m_blnC1GoUp = False
    3.         ElseIf m_intAntiBlueC1 <= 0 Then
    4.             m_blnC1GoUp = True
    5.         End If
    6.  
    7.         If m_blnC1GoUp Then
    8.             m_intAntiBlueC1 += 3
    9.         Else
    10.             m_intAntiBlueC1 -= 3
    11.         End If
    12.  
    13.         Dim graphics As Graphics
    14.         graphics = Me.CreateGraphics()
    15.         Dim c1 As Color = Color.FromArgb(m_intAntiBlueC1, m_intAntiBlueC1, 200)
    16.         Dim c2 As Color = Color.FromArgb(255 - m_intAntiBlueC1, 255 - m_intAntiBlueC1, 200)
    17.         Dim brush As New Drawing2D.LinearGradientBrush(Me.DisplayRectangle, c1, c2, Drawing2D.LinearGradientMode.ForwardDiagonal)
    18.         graphics.FillRectangle(brush, Me.DisplayRectangle)
    19.         Dim TheBit As New Bitmap(295, 80)
    20.         Dim graphics2 As Graphics
    21.         graphics2 = Drawing.Graphics.FromImage(TheBit)
    22.  
    23.        
    24.         'txtDebug.Text = txtDebug.Text & vbNewLine & c1.R.ToString & "," & c1.G.ToString & "," & c1.B.ToString & "   " & c2.R.ToString & "," & c2.G.ToString & "," & c2.B.ToString
    25.        
    26.         brush = New Drawing2D.LinearGradientBrush(lblLogo.DisplayRectangle, c1, c2, Drawing2D.LinearGradientMode.Horizontal)
    27.         graphics2.FillRectangle(brush, 0, 0, 295, 80)
    28.         lblLogo.Image = TheBit
    29.  
    30.         'If m_intAntiBlueC1 = 0 Then
    31.         '    tmrGradient.Enabled = False
    32.         '    'TheBit.Save("peak2.bmp")
    33.         'End If
    and declare the module rgb variables at module lvl
    Last edited by Desolator144; Nov 2nd, 2006 at 08:14 PM.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width