Re: Exceptions occuring related to Graphics
From: Christopher Secules (secules_at_pica.army.mil)
Date: 11/23/04
- Next message: Thomas Bock: "Grid"
- Previous message: Frank Hileman: "Re: Problem in GDI+"
- In reply to: Bob Powell [MVP]: "Re: Exceptions occuring related to Graphics"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 23 Nov 2004 09:23:44 -0500
The only threading I am using is with a sub to send out command reponses
over the serial port. That sub does not use the bitmap or graphics class.
I do have a handler for my com events when I receive bytes. There is one
situation where the handler draws text on the bitmap. Not sure if the
handler would execute in its own separate thread though.
Yes, I am eventually saving it back to the same filename after the end of
every function that draws something on it. I was hoping on getting around
that issue by loading it from the file to an Image first, then creating a
new bitmap out of the image, and finally disposing of the image. Wouldn't
that free up the ties to that file so I can freely make changes and
eventually save it back to that same file?
Below is a few code segments. The project is an emulator that is emulating
a pre-existing display that is 160x80 pixels. I'll try to include what
might be relevant. Let me know if I've missed something. Again, these
exceptions are completely random. Sometime is works for a good minute or
two, other times it quits immediately.
Thanks for the help,
Chris
*** Object Constructor ***
Public Sub New()
Dim temp_image As Image = Image.FromFile(IMAGE_DIR & "current.bmp")
Current_Screen_Bitmap = New Bitmap(temp_image)
Current_Screen_Copy = Current_Screen_Bitmap
temp_image.Dispose()
ClearScreen()
End Sub
*** Routine to clear the screen and set every pixel to the color of the
background. No exception have ever occurred in here. ***
Public Sub ClearScreen()
Try
Dim temp_image As Image = Current_Screen_Bitmap
Dim temp_bitmap As New Bitmap(temp_image)
temp_image.Dispose()
Dim temp_graphics As Graphics = Graphics.FromImage(temp_bitmap)
temp_graphics.Clear(GND_backColor)
temp_graphics.Dispose()
temp_bitmap.Save(IMAGE_DIR & "current.bmp", Imaging.ImageFormat.Bmp)
Current_Screen_Bitmap = temp_bitmap
LogToFile("GND Display needs to be updated")
m_Update_Display = True
Catch ex As Exception
LogToFile("Error occured in ClearScreen: Error: " & ex.Message & ";
Inner Error: " & ex.InnerException.Message & "; Stacktrace: " &
ex.StackTrace)
End Try
End Sub
*** This is one of the Routines that was causing problems. Got the
Exception "The object is currently in use elsewhere." when trying to load
temp_bitmap_Line from temp_image.
*** The routine draws a line on the screen, but I couldn't use the built in
Graphics class to draw the line, as one of the possible commands from the
controlling computer is to set the line pattern to a repeating 16 bit
pattern.
Public Sub DrawLine(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As
Integer, ByVal y2 As Integer)
Dim temp_image As Image = Current_Screen_Bitmap
Dim temp_bitmap_Line As Bitmap
temp_bitmap_Line = New Bitmap(temp_image)
temp_image.Dispose()
Dim temp_graphics As Graphics = Graphics.FromImage(temp_bitmap_Line)
Dim temp_pattern As String = DectoBin(Line_Pattern)
For count2 As Integer = y1 To y2
For count1 As Integer = x1 To x2
If temp_pattern.Chars(count1 Mod 16) = "1" Then
temp_bitmap_Line.SetPixel(count1, count2,
GND_textColor)
Else
temp_bitmap_Line.SetPixel(count1, count2,
GND_backColor)
End If
Next
Next
temp_graphics.Dispose()
temp_bitmap_Line.Save(IMAGE_DIR & "current.bmp",
Imaging.ImageFormat.Bmp)
Current_Screen_Bitmap = temp_bitmap_Line
m_Update_Display = True
End Sub
*** This is the text drawing routine. Again, I couldn't use the built in
Graphics Class to draw text. I couldn't find a font that matched what I
wanted, so I created it based on pixel interpretation from the actual
hardware display.
*** PixelCharMap is a HashMap of string arrays. The strings are either 8 or
6 characters of 1's and 0's, based on the current font size (0 or 1).
*** The exception in this routine was "Object reference not set to an
instance of an object.", and it occurred in the same spot as the other
one...with loading the temp_bitmap from the temp_image.
Public Sub DrawText(ByVal message As String)
Dim temp_bitmap As Bitmap
Dim temp_image As Image = Current_Screen_Bitmap
temp_bitmap = New Bitmap(temp_image)
temp_image.Dispose()
Dim xIndex As Integer = 0
Dim yIndex As Integer = 0
Dim ascii As System.Text.Encoding = System.Text.Encoding.ASCII
Dim text_bytes() As Byte = ascii.GetBytes(message)
For count As Integer = 0 To text_bytes.Length - 1
Dim temp_char() As String =
PixelCharMap(CStr(text_bytes(count)) & CStr(m_FontChosen))
yIndex = 0
For count2 As Integer = m_Cursor_YPos To m_Cursor_YPos +
temp_char.Length - 1
xIndex = 0
For count1 As Integer = m_Cursor_XPos To
m_Cursor_XPos + temp_char(yIndex).Length - 1
If temp_char(yIndex).Chars(xIndex) = "1"
Then
If m_Video_Mode = 0 Then
temp_bitmap.SetPixel(count1,
count2, TextColor)
ElseIf m_Video_Mode = 1 Then
temp_bitmap.SetPixel(count1,
count2, GND_backColor)
End If
Else
If m_Video_Mode = 0 Then
temp_bitmap.SetPixel(count1,
count2, GND_backColor)
ElseIf m_Video_Mode = 1 Then
temp_bitmap.SetPixel(count1,
count2, TextColor)
End If
End If
xIndex += 1
Next
yIndex += 1
Next
m_Cursor_XPos = m_Cursor_XPos + temp_char(0).Length
Next
temp_bitmap.Save(IMAGE_DIR & "current.bmp", Imaging.ImageFormat.Bmp)
Current_Screen_Bitmap = temp_bitmap
m_Update_Display = True
End Sub
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:uavA0FU0EHA.3588@TK2MSFTNGP14.phx.gbl...
> Are you using threading? Are you cacheing Graphics objects anywhere? Are
you
> saving the image back to the same filename?
>
> Can you post some code please. Your description is a bit vague.
>
> --
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Find great Windows Forms articles in Windows Forms Tips and Tricks
> http://www.bobpowell.net/tipstricks.htm
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/faqmain.htm
>
> All new articles provide code in C# and VB.NET.
> Subscribe to the RSS feeds provided and never miss a new article.
>
>
>
>
>
> "Christopher Secules" <secules@pica.army.mil> wrote in message
> news:%23SWyZLO0EHA.4004@tk2msftngp13.phx.gbl...
> > I am writing an application that uses remoting and the GDI graphics
class
> to
> > update a bitmap that is getting displayed on the Client (UI). The
server
> > receives commands from a controller over the serial port, and when a
> > complete command comes in, it interprets it. Interpretation of these
> > commands might involve the GDI graphics class. For example, commands
> > include drawing a line, drawing a circle, and drawing text. The bitmap
> gets
> > updated after every action, and then the Client senses the change and
> upates
> > itself.
> >
> > I believe I am running into a problem with drawing on the bitmap.
> Randomly,
> > I get 1 of 2 possible exceptions:
> >
> > The object is currently in use elsewhere.
> >
> > and
> >
> > Object reference not set to an instance of an object.
> >
> > Normally, I would be able to track down where it is originating.
However,
> > the InnerException was not set and the Stacktrace didn't come up with
> > anything useful. I only thought is that commands are coming in over the
> > serial port too fast and things are getting tied up. Any suggestions to
> > either get to the bottom of this, or to fix it? I was considering a
> > Synclock around my command interpretation routine.
> >
> > Thanks.
> >
> >
>
>
- Next message: Thomas Bock: "Grid"
- Previous message: Frank Hileman: "Re: Problem in GDI+"
- In reply to: Bob Powell [MVP]: "Re: Exceptions occuring related to Graphics"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|