Help with OnPaint
-
Currently, I have this code in my on paint event in a view.
CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } }
As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the followingCPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...)
However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)"); -
Currently, I have this code in my on paint event in a view.
CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } }
As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the followingCPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...)
However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");You need to allocate a bitmap (ideally a DIBSection) of the appropriate size and select it into the device context. The default bitmap in a freshly created DC is 1x1. If you use a DIBSection, you can get direct access to the bitmap's pixel data. This can dramatically speed up pixel manipulation.
-
Currently, I have this code in my on paint event in a view.
CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } }
As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the followingCPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...)
However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");The effective way of createing a memory dc.
HDC memDC = CreateCompatibleDC ( hDC ); HBITMAP memBM = CreateCompatibleBitmap ( hDC ); SelectObject ( memDC, memBM );
Interviewer (me) : "Did you install Linux all my yourself?" Candidate : "Yes i installed Linux all my yourself."
-
Currently, I have this code in my on paint event in a view.
CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } }
As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the followingCPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...)
However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");Go look in the "Bitmaps & Pallettes" section of codeproject; both CDibSectionLite and CDibData provide ways of accessing pixels directly without having to select them into a DC first. Note: Accessing pixel data through any DC is very slow. INTP
-
Currently, I have this code in my on paint event in a view.
CPaintDC dc(this); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { dc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } }
As you can understand, this is pretty slow with soewhat bigger pictures where for example, tje size is over 1000 x 1000 pixels. A solution, as far as I know, is to use a memory DC. However, I am doing something wrong. I want to create the memory DC with the followingCPaintDC dc(this); CDC pdc; dc.CreateCompatibleDC(&pdc); CPoint originalSize = matrix.GetSize(); int y, x; for (y = 0; y < size.y; y++) { for (x = 0; x < size.x; x++) { pdc.SetPixelV(CPoint(x,y), obj.GetColorRef(CPoint(x, y))); } } dc.BitBlt(0, 0, size.x, size.y, ...)
However, the first time only a black block was drawn then, when resizing things go wrong (the app crashes), where a colored block should be drawn. What am I doing wrong, and if I am doing it wrong, how should I do it? LPCTSTR Dutch = TEXT("Double Dutch :-)");As was suggested before you need to create a compatible bitmap. You also need to pass the dc from the paint DC to the CreateCompatibleDC function, not the other way around. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke