|
If you think that style is causing a problem, create another control without it and see what happens.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
|
i load an Cimage file in my SDI MFC (non View/Doc arch.) application & draw some shapes & text on it... tell me how i can save both shapes,texts & CImage in any (jpg,bmp) file format...
|
|
|
|
|
I got the answer to this problem while trying to solve my problem:
CImage image;
HRESULT hr = image.Save(_T("C:\\test.jpg"), Gdiplus::ImageFormatJPEG);
|
|
|
|
|
i'm drawing shapes on it(like circle,rectangle etc) using OnPaint(); .... will it work now??
|
|
|
|
|
No; when you use the CDC in OnPaint, your drawing is going to the screen.
To draw on a bitmap you need to select the bitmap into a device context, then draw on that device context. Like:
CDC *mdc = GetDC ();
HGDIOBJ tmp = mdc->SelectObject(hbm);
image.Attach (hbm);
HRESULT hr = image.Save(_T("C:\\test.tif"), Gdiplus::ImageFormatTIFF);
|
|
|
|
|
i'm not using cdc....i'm using CPaintDC dc...so now wht i have to do??
|
|
|
|
|
CPaintDC is derived from CDC, so you can perform all the same operations on it. It's an object rather than a pointer, so you have to use "." to invoke methods instead of "->".
You can select a bitmap into the CPaintDC:
dc.SelectObject (hbm);
Where hbm is the handle of a bitmap. You can also select a pointer to a CBitmap. (I don't see an overload for CImage).
You then use the drawing methods of CDC. Then (as in my example) you can attach your bitmap to a CImage object, and save it.
There may be a better way of doing this, but this should work.
|
|
|
|
|
what is hbm? plz write full code in detail...
|
|
|
|
|
hbm is a handle to a bitmap. The Windows type is HBITMAP.
|
|
|
|
|
Runtime error: tht hbm is not initialized....?
|
|
|
|
|
|
Hi all,
I've been attempting to draw on an 8bpp grayscale bitmap without success. Here are some of my attempts. Maybe someone can point out what I'm doing wrong.
===================================================
Attempt 1: Create, select, and draw:
In constructor:
CBitmap bm;
bm.CreateBitmap (200, 200, 1, 8, NULL);
In OnDraw:
CDC *mdc=new CDC ();
HGDIOBJ tmp = mdc->SelectObject(bm);
Result: tmp is NULL, indicating failure.
===================================================
Attempt 2: CreateDIBSection
In constructor:
HBITMAP hbm;
BITMAPINFOHEADER bih;
BITMAPINFO bi;
HANDLE hb;
CDC* myDc = new CDC ();
HDC hdc = myDc->GetSafeHdc ();
void* bits;
RGBQUAD rq [256];
initBi ();
hbm = CreateDIBSection (hdc, &bi, DIB_RGB_COLORS, &bits, NULL, 0);
...
void CEightBitDrawingView::initBi()
{
bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = 200;
bih.biHeight = -200;
bih.biPlanes = 1;
bih.biBitCount = 8;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 14173;
bih.biYPelsPerMeter = 14173;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
memset ((void *) rq, 0, 256 * sizeof (RGBQUAD));
bi.bmiHeader = bih;
bi.bmiColors = rq;
}
Result: This doesn't even compile because the BITMAPINFO bmiColors member is defined as:
RGBQUAD bmiColors[1];
so won't accept more than one RGB color. In fact, nothing I assign to this member compiles! (Could they possibly make it any more complex!?)
Any suggestions would be appreciated! Thanks!
===================================================
|
|
|
|
|
Alan Balkany wrote: CDC *mdc=new CDC ();
HGDIOBJ tmp = mdc->SelectObject(bm);
- do you create the DC anywhere or you just instantiate it? You need to create the DC, a CDC instance alone isn't enough. Where does bm come from? Acording to what you said bm would be a local variable in the constructor, so how would it get to OnDraw?
About the RGBQUAD thing:
Alan Balkany wrote: RGBQUAD bmiColors[1];
this means that an array of RGBQUADs containing a variable number of colors will follow directly the BITMAPINFO structure. I think the biClrUsed member would indicate this in the struct (see the documentation for the structure).
You have to allocate enough memory to contain the bitmap info struct AND the palette after it, you can set the members in the palette -if you like- thorough the bmiColors array. Something like:
BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 255);
...fill the bmiHeader ...
bmi->bmiCOlors[0] = firstColor;
bmi->bmiCOlors[1] = secondColor;
...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Code-o-mat, thanks for clearing up the mystery about bmiColors!
I just created a new DC because (it appears that) existing DCs will default to the color depth of my display. If this isn't correct, what's the right way to obtain a DC to draw on an 8bpp bitmap?
(The bitmap is actually declared in the class, and was just included in the constructor's code to show the type.)
|
|
|
|
|
I needed to do this some time ago and as far as i can remember i went the DIB section way myself, creating a palette that went from 0,0,0 to 255,255,255. If you create a screen-compatible DC and then select your bitmap into it i think you should be able to draw on it.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Thanks for the help, Code-o-mat! I can now generate bitmaps, but they're coming out incorrectly.
Short version: I created an 8bpp bitmap, then I BitBlt it to the screen and save it into a file. The copy on the screen has color, which suggests it's not just 8bpp. The version saved to the file is 8bpp, but it's totally black, where it should have at least *some* of the drawing.
Question: How can I draw on an 8bpp grayscale bitmap and save it to a file? Can anyone see what I'm doing wrong?
========================================================
The Details
Declarations:
CBitmap bm;
HBITMAP hbm;
BITMAPINFO* bi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER bih;
Constructor:
initBi ();
hbm = CreateDIBSection (hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
...
void CEightBitDrawingView::initBi()
{
bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = 200;
bih.biHeight = -200;
bih.biPlanes = 1;
bih.biBitCount = 8;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 14173;
bih.biYPelsPerMeter = 14173;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
bi->bmiHeader = bih;
for (int i = 0; i < 256; i++)
{
bi->bmiColors [i].rgbRed = i;
bi->bmiColors [i].rgbGreen = i;
bi->bmiColors [i].rgbBlue = i;
}
}
OnDraw:
void CEightBitDrawingView::OnDraw(CDC* pDC)
{
CClientDC dc(this);
CDC *mdc = GetDC ();
HGDIOBJ tmp = mdc->SelectObject(hbm);
mdc->FloodFill (0, 0, RGB (255, 255, 255));
CBrush* br = new CBrush (RGB (255, 0, 255));
CRect rect (30, 60, 130, 160);
mdc->FillRect (&rect, br);
mdc->TextOutW (50, 50, L"Testing...");
CImage image;
image.Attach (hbm);
HRESULT hr = image.Save(_T("C:\\test.tif"), Gdiplus::ImageFormatTIFF);
GetClientRect(&rect);
dc.BitBlt(0,0,rect.right,rect.bottom, mdc, 0,0,SRCCOPY);
mdc->SelectObject (tmp);
delete br;
}
|
|
|
|
|
Am not sure where you are going wrong, i did a quick proggie to check and it produced a correct bitmap:
BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER &bih(bmi->bmiHeader);
bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = 200;
bih.biHeight = -200;
bih.biPlanes = 1;
bih.biBitCount = 8;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 14173;
bih.biYPelsPerMeter = 14173;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
for (int I = 0; I < 255; I++)
{
bmi->bmiColors[I].rgbBlue = bmi->bmiColors[I].rgbGreen = bmi->bmiColors[I].rgbRed = (BYTE)I;
bmi->bmiColors[I].rgbReserved = 0;
}
void *Pixels;
HBITMAP hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &Pixels, NULL, 0);
BYTE *Pix = (BYTE *)Pixels;
for (int Y = 0; Y < abs(bih.biHeight); Y++)
for (int X = 0; X < bih.biWidth; X++, Pix++)
*Pix = (BYTE)Y;
free(bmi);
CImage Image;
Image.Attach(hbmp);
Image.Save(_T("r:\\test.bmp"), Gdiplus::ImageFormatBMP);
DeleteObject(hbmp);
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
I also added this:
HDC dc = ::GetDC(NULL);
HDC memdc = CreateCompatibleDC(dc);
::ReleaseDC(NULL, dc);
HGDIOBJ Ofont = ::SelectObject(memdc, GetStockObject(DEFAULT_GUI_FONT));
HGDIOBJ Obmp = ::SelectObject(memdc, hbmp);
::SetTextColor(memdc, RGB(128, 128, 128));
::SetBkColor(memdc, RGB(64, 64, 64));
::TextOutA(memdc, 2, 2, "Hello world", 11);
::SelectObject(memdc, Ofont);
::SelectObject(memdc, Obmp);
DeleteDC(dc);
between free(bmi); and CImage Image; , this works fine too, i get the text in grays.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Thanks again Code-o-mat! This gives me a lot to experiment with!
|
|
|
|
|
No probs, i hope it helps, good luck.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
It worked!! Thanks so much! A big 5 points for you! (I'd give you more if I could.)
|
|
|
|
|
Great , glad i could help, thanks for the points.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Ok I know this is a bit of a cheat but my UI is split into 3rds with the idea being I can hide the bits of the UI.
Now I can slice off the right hand side easily enough as follows.
...
CRect rect;
GetWindowRect(rect);
...
rect.right += CONSOLE_SIZE;
MoveWindow(rect, 1);
...
The problem I am having is how do I cut the dialog's window down so that only the middle 3rd is displayed? I have had a look at CRgn with the SetWindowRgn but I am getting nowhere.
Any suggestions would be appreciated.
Many thanks
Alan
|
|
|
|
|
You could try making your dialog a child of another dialog. You'd have a popup dialog and inside its client area a child dialog with its controls, thus you can shift this child dialog inside the popup's client area to show the part you wish.
On a sidenote: i don't know what kind of controls you have on your dialog, but if you use the keyboard, you can tab around controls even if they are not visible because they are "outside" of the visible client area. So if you tab onto a button you can press it by using SPACE even though you don't see it.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|