Results 1 to 9 of 9

Thread: Drawing TGA on Vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    8

    Drawing TGA on Vb.net

    Ok, I have a problem. I wanted to show TGA files on my application. So I manage to code up something that does this. I have the TGA file loaded up in an array and decoded properly. But when I use a simple looping to go through the array at each pixel I set a brushcolor then use a fillrectangle, not the most efficient way. But just to get something quick and simple to 1st try.

    The problem is that when I try to draw the image on a picture box control for even a 256x256 image, it is very slow. Even slower then I would expect for the way I did it. It looks like the brush color setting and draw routines are very slow. The loops themselves doesnt take much time which i wouldnt expect them to. The question is, is there some way to have the image rendered quickly, once I have it loaded into memory? I tried using bitmap class and that wasnt much help either.


    The second problem is that I am trying to draw the image on a picturebox control. The image is selected by other controls on the form, which also places different buttons depending on what has been selected (which may include the picturebox). The problem is that the picture box seems to be rendered firstly out of sequence, which isnt a big problem in itself, just wierd. But the main problem is that the picturebox resets to the background after the other controls are placed on the form.

    also what is strange is that even though i put the rendering code in the picturebox's paint event. it doesnt refresh the screen properly either. like when i cover up part of it. it refreshes but NOT that region that was blocked.

    also the image seems to revert back to the background or image (that is default in the property and is a solid grayish color) just after it exits the paint event handler for the picturebox. so its like if the painting initially on the picturebox is happening before it does its default action and the image isnt being actually put into the picturebox's image or background property. cause when i try painting directly on the form with the same rendering routine instead the image doesnt dissappear.


    here is what i am doing in the rendering routine:

    public sub renderTGAImage(pb as picturebox, image as TGAimage)
    Dim pbGraphics As System.Drawing.Graphics
    pbGraphics = pb.CreateGraphics
    for y = 0 to (height - 1)
    for x = 0 to (width - 1)
    ialpha = TGAimage.A(j)
    ired = TGAimage.R(j)
    igreen = TGAimage.G(j)
    iblue = TGAimage.B(j)
    Dim PenColor As New Pen(Color.FromArgb(ialpha, ired, igreen, iblue))
    PPGraphics.DrawLine(PenColor, x, y, x+1, y+1)
    j +=1
    next
    next

    Private Sub pbPreviewpane_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbPreviewPane.Paint
    Dim obj As PictureBox = CType(sender, PictureBox)
    RenderTGAImage(obj, ImageArray)
    End Sub <= break point here is when the image reverts back to background

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    I don't know if this will be faster, but it's worth a try.

    VB Code:
    1. Dim B as New Bitmap(256,256)
    2.  
    3. for y = 0 to (height - 1)
    4. for x = 0 to (width - 1)
    5. ialpha = TGAimage.A(j)
    6. ired = TGAimage.R(j)
    7. igreen = TGAimage.G(j)
    8. iblue = TGAimage.B(j)
    9. Dim C As Color=Color.FromArgb(ialpha, ired, igreen, iblue)
    10. B.SetPixel(x,y,c)
    11. j +=1
    12. next
    13. next
    14.  
    15. pb.image=b
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    8
    hmmm well the speed didnt improve much
    but it fixed the problem of the inage dissappearing after it is drawn. problem is though it seems to make everything sluggish. its like if its very busy doing something, and cant service any other controls and basically unuseable

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    The application will not respond to anything until the loops are finished.
    You could put an Application.Doevents inside the loops. That would fix it, but it would make it even slower.

    Perhaps you could make it in a new thread.
    Making a thread doesn't make the image processing faster, but your app will respond to events while waiting.

    I made a small sample below, but I couldn't really tell the difference because on my PC it only took 79 milliseconds to complete.

    But ofcourse, I don't have TGA to try with so I'm just toggling the color for each pixel. Accessing your ARGB arrays will ofcourse make it slower.


    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim T As Threading.Thread
    3.  
    4.         T = New Threading.Thread(AddressOf MyThread)
    5.         T.Start()
    6.  
    7.     End Sub
    8.  
    9.     Private Sub MyThread()
    10.         Dim B As New Bitmap(255, 255)
    11.         Dim X, Y As Integer
    12.         Dim C As Color = Color.Red
    13.  
    14.         For Y = 0 To B.Height - 1
    15.             For X = 0 To B.Width - 1
    16.                 B.SetPixel(X, Y, C)
    17.                 If C.R = 255 Then
    18.                     C = Color.Blue
    19.                 Else
    20.                     C = Color.Red
    21.                 End If
    22.             Next
    23.         Next
    24.  
    25.         PictureBox1.Image = B
    26.     End Sub
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    8
    i'll give that ia try but is there a way to pass arguments into the new thread?

    hmmm also 79 millisec? for a 256x256 it is taking me a full 5 seconds to draw. why is it so slow? This is even if I just try a checker like u are and not loading the actual image.

    the way i did it originally using pen and drawline was actually faster. but one wierd thing is why does the image dissappear after being painted on the picturebox? (it doesnt dissappear if i use the setpixel method though).
    Last edited by raytang1009ca; Feb 28th, 2004 at 01:26 AM.

  6. #6
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    5 secs huh? that seems like a long time. What are your PC specs?
    Mine is a Celeron 2.8ghz with 512 ram.


    but one wierd thing is why does the image dissappear after being painted on the picturebox? (it doesnt dissappear if i use the setpixel method though).
    This is because when you create a graphics object of the picturebox you are drawing directly on top of the picturebox.
    This means that when the picturebox is refreshed the picturebox redraws itself like nothing happend.
    So to get the image to stay, you would have to redraw the image your self in the paint event of the picturebox.

    The way I showed draws to a bitmap object and assigns that bitmap to the image property of the picturebox, telling the picturebox to draw this image every time it is refreshed.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    8
    right ok....thats kinda what i thought
    also why is it so slow to draw? is there a way to just have the image property just point to an array or something to cut down the time? my system spec is P4-Intel 2.53 Ghz, 1 Gig Ram so basically no reason on the hardware side. I am running it in debug mode but still it shouldnt be that slow

  8. #8
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Well, it's definitly not the hardware
    I ran my test in debug mode as well, so that can't be it...

    Are there some other process running on the pc that could cause the slowness?
    I can't think of any other reason why it would be so slow.

    Are all your VB Net prog's this slow or is it just this one procedure?
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    8
    well since the last time, i'm basically doing what i did originally with the fillrectangle method to draw it (which takes about a second, i cheat for larger images by reducing the resolution and keeping the compressed format and drawing lines instead of a single pixel) then also draw it again with setpixel which takes longer but keeps the image on the picturebox.

    For smaller images, its pretty quick for a previewer like i am doing but still slow for games
    Last edited by raytang1009ca; Feb 29th, 2004 at 03:13 AM.

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