canvas does not allow drawing

Posted on 2000-04-26
Last Modified: 2008-03-04
I am double-buffering a bitmap and draw it on a form's canvas. It works fine without flicker, but after running my program for a while, I suddenly get the "canvas does not allow draing" error message. how do I solve this?
Question by:TropicalFish
  • 8
  • 3
  • 3

Expert Comment

ID: 2752926
I am guessing that perhaps, you have done of  a few different things.

1. you have not freed the offscreen bitmaps, thus depleting GDI resources.

2. You have locked the form's canvas.

any of this sound likely?

good luck!!

Author Comment

ID: 2753146
Yes, I havn't freed the bitmaps. Could you please tell me how to do this (with code). I am using one bitmap as background, and another one that I constantly load different images from a resource into. I draw that bitmap on the background image several times, before I draw the background on the form's canvas. This works for about 100 screen updates, but then the memory probably runs out. How do I free it? Thanks.

Expert Comment

ID: 2753191
  what incarnation of C++ are you using? (C++ Builder, VC++, etc..?);
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

ID: 2753206
I'm using C++ Builder 4.

Expert Comment

ID: 2753243
Assuming you are using TBitmaps (and I am assuming that you are in BCB), your loop would look like this:

bmp =new TBitmap;
 //drawing code goes here


Put this in your loop and you should be fine..

Good luck!!

Author Comment

ID: 2753324
It sais: E2316 'free' is not a member of 'TBitmap'.

Author Comment

ID: 2753366
Ok, now I'm using "bmp->Free()" to free all my bitmaps, but I still get the "wrong parameter" and "canvas does not allow drawing" after a while, plus now I also get a Win32 error code: 2. I'm confused.
LVL 14

Expert Comment

ID: 2754095
This exception is raised when an attempt is made to use the Handle
property of the Canvas when its handle is still invalid. That is, the
Canvas does not correspond to a valid device context. For example, the
following will raise the EInvalidOperation exception...

#include <memory>

std::auto_ptr<TCanvas> TempCanvas(new TCanvas());

Since most TCanvas methods wrap a GDI function, and nearly all GDI
functions require a handle to a valid device context, simple operations
such as the following will also fail...

std::auto_ptr<TCanvas> TempCanvas(new TCanvas());


std::auto_ptr<TCanvas> TempCanvas(new TCanvas());
TempCanvas->Brush->Color = clRed;

The following however, will work, since the Handle property of the
TempCanvas is specified as the handle to the device context of a valid
windowed control...

std::auto_ptr<TCanvas> TempCanvas(new TCanvas());
TempCanvas->Handle = GetDC(Button1->Handle);
ReleaseDC(Button1->Handle, TempCanvas->Handle);

For test your situation, post snippet
of you code.


Author Comment

ID: 2754666

Graphics::TBitmap *lcd = new Graphics::TBitmap;
Graphics::TBitmap *knappbild = new Graphics::TBitmap;

Graphics::TBitmap *background = new Graphics::TBitmap;

Graphics::TBitmap *backbuffer = new Graphics::TBitmap;

backbuffer->Width = background->Width;
backbuffer->Height = background->Height;
lcd->Transparent = true;
knappbild->Transparent = true;

void __fastcall TForm1::FormPaint(TObject *Sender)

lcd->LoadFromResourceID((int)HInstance,MarkPos + 1300);

if (FallPos < 4){
lcd->LoadFromResourceID((int)HInstance,FallPos + 1304);

if (FallPos != 0 && FallPos != 3) {lcd->LoadFromResourceID((int)HInstance,UpPos + 1500);
if (FallPos != 1 && FallPos != 2) {lcd->LoadFromResourceID((int)HInstance,NedPos + 1600);
lcd->LoadFromResourceID((int)HInstance,ental + 1200);
if (tiotal << 0 || hundratal << 0) {lcd->LoadFromResourceID((int)HInstance,tiotal + 1200);
if (hundratal << 0) {lcd->LoadFromResourceID((int)HInstance,hundratal + 1200);

if (Miss > 0) {lcd->LoadFromResourceID((int)HInstance,Miss + 1210);

if (knpner[0] == 1) backbuffer->Canvas->Draw(32,158,knappbild);
if (knpner[1] == 1) backbuffer->Canvas->Draw(32,202,knappbild);
if (knpner[2] == 1) backbuffer->Canvas->Draw(314,203,knappbild);
if (knpner[3] == 1) backbuffer->Canvas->Draw(314,159,knappbild);

LVL 14

Expert Comment

ID: 2755212
I have some remarks to you code:
1. After using must free! as
void __fastcall TSwatForm::FormDestroy(TObject *Sender)
    delete lcd;
    delete backbuffer ;
2. Test MarkCord,.., FallCord arrays:
   may by values are invalid
3. You use LoadFromResourceID function
   if ID (as NedPos + 1600) is invalid
: no resource with this ID, Draw fails
 Test all resources, espesially before
 error message!
4.  What meens
>>  if (hundratal << 0) ?
it is same as
if (hundratal)

5. And last : i don't like Load some
   bitmap during paint!
   Better way is : load during changings
   parameters (as FallPos for example),
   and paint deja loaded Bitmaps.



Author Comment

ID: 2755718
Um.. it seems that "if(hundratal << 0)" does the same thing as "if(hundratal > 0)". Work both ways.. means bigger than 0. The MarkCord and FallCord arrays are fine, and all resources work up till the error point. I'll look into your points 1 and 5 though.

Also, I have put up my executable at:
If you have the time, maybe you want to take a look at it, it's a 307kb download. Start the game on gameA and play it with q,s,8,4 keys up till about 30 points (sometimes longer) Then you'll see the errors.

Author Comment

ID: 2756813
Adjusted points from 50 to 75

Author Comment

ID: 2756814
Now I have changed it so no bitmaps are loaded during paint, like you said. No difference. Only that I need to use 12 bitmaps instead of four, otherwise it writes over itself before drawing it to the backbuffer. I increased the points for this question, I really need this to workout!
LVL 14

Accepted Solution

AlexVirochovsky earned 75 total points
ID: 2758608
I try you exe and get error:
borlnmm.dll missing.
But if I have get all dll, without source text I can't make debug!
And I think, that debug it is you
part. My part: make advice.
I think, that you must test punkt 3 from
my previous remark.
Use try /catch for find exect place
of error. In this moment test
all ID and arrays.

Other way: make intermediate printing
in file during every Paint and see
values , may be find some strange

And test system resources,may be
in some other part of text
you make "new" without "free"
or go out of array bound.


Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question