在基于dialog的MFC中使用gdi+的问题

Chendy1985 2008-10-22 04:51:43
怎么用GDI+加载资源中的bitmap?
我新建一个基于dialog的MFC工程,一定要是dialog的,然后在资源里添加一个bitmap
我想在把这张bitmap贴到dialog上去(在GDI下可以用StretchBlt贴,但是在GDI+呢?用DrawImage?)
怎么才能贴上去啊?我用DrawImage搞了一天了都没有搞定,什么效果都没有.......
哪位大侠给讲一下吧,最好有关键的代码
...全文
346 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Chendy1985 2008-10-27
  • 打赏
  • 举报
回复
10樓 不是我想將在資源,是要求加載資源里的
而且有些地方用資源里的東西比較安全一點,放在外面用戶不小心刪掉了豈不就玩完了.....
xlh67xlh67xlh67 2008-10-23
  • 打赏
  • 举报
回复
同意10楼的方法。
Chendy1985 2008-10-23
  • 打赏
  • 举报
回复
还是不行啊
帮我看看代码吧

HDC hdc;
PAINTSTRUCT ps;

hdc = ::BeginPaint(m_hWnd, &ps);

Graphics grap(hdc);

Bitmap pBitmap(NULL, MAKEINTRESOURCE(IDB_ALPHA));
grap.DrawImage(&pBitmap, 10, 10);

::EndPaint(m_hWnd, &ps);


谢谢啦
lirongjun1985 2008-10-23
  • 打赏
  • 举报
回复
用GDI+不用非得加载到资源里面去,下面是一种方法。
CClientDC dc(this);
Graphics graphics(dc.m_hDC);
Image img(L"D:\\lrj\\testproject\\Picture\\**.jpg");或其他格式的图片
graphics.DrawImage(&img,Rect(10,10,img.GetWidth(),img.GetHeight()));
chenyu2202863 2008-10-23
  • 打赏
  • 举报
回复
去找本翻译了的GDI+资料吧,是MSDN的~
初学够用!

《GDI+SDK参考(翻译版本).chm》
Tinary3v0 2008-10-23
  • 打赏
  • 举报
回复
在Dlg的OnPaint里面实现的代码:

void CUSDLG::OnPaint() 
{
CPaintDC dc(this);
Graphics graphics(dc.m_hDC);
HINSTANCE hInstance = ::AfxGetResourceHandle();
Bitmap bitmap(hInstance, MAKEINTRESOURCEW(IDB_HUT));
CachedBitmap CbtMap(&bitmap,&graphics);
graphics.DrawCachedBitmap(&CbtMap,20,20);
}
hyg 2008-10-23
  • 打赏
  • 举报
回复
从你的资源加载图片到Image对象
Image *pImage=NULL;
BOOL bResult = theApp.ImageFromIDResource(IDB_YOUR_BITMAP,_T("Bitmap"),pImage);
hyg 2008-10-23
  • 打赏
  • 举报
回复
加载资源中的Bitmap

BOOL YourProjectApp::ImageFromIDResource(UINT nID, LPCTSTR sTR,Image * &pImg)
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
if (!hRsrc)
return FALSE;

// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE;

// Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
GlobalUnlock(m_hMem);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);

// load from stream
pImg=Gdiplus::Image::FromStream(pstm);

// free/release stuff
pstm->Release();
FreeResource(lpRsrc);
GlobalFree(m_hMem);

return TRUE;
}
hyg 2008-10-23
  • 打赏
  • 举报
回复
定义一个Bitmap
Bitmap *m_pBitmap;
m_pBitmap = NULL;

1、在OnInitialize()中初始化图片

if (m_pBitmap)
{
::delete m_pBitmap;
m_pBitmap = NULL;
}
Image *pImage=NULL;

USES_CONVERSION;
#ifdef UNICODE

pImage = Image::FromFile(strPicName);//从文件加载图片
#else
pImage= Image::FromFile(A2W(strPicName));
#endif

if (pImage)
{
int nWidth = pImage->GetWidth(),nHeight = pImage->GetHeight();
m_pBitmap = ::new Bitmap(nWidth,nHeight);
Graphics graphicsBitmap(pImage);

graphicsBitmap.DrawImage(pImage,RectF(0,0,nWidth,nHeight),0,0,nWidth,nHeight,UnitPixel);
SAFE_DELETE(pImage);
graphicsBitmap.ReleaseHDC(NULL);
}



2、在OnPaint中绘制你的图片

if(m_pBitmap)
{
HDC hHdc = GetDC()->m_hDC;
Graphics graphics(hHdc);
int nWidth = m_pBitmap->GetWidth(),nHeight = m_pBitmap->GetHeight();
graphics.DrawImage(m_pBitmap,RectF(0,0,nWidth,nHeight),0,0,nWidth,nHeight,UnitPixel);
graphics.ReleaseHDC(hHdc);
}
一条晚起的虫 2008-10-22
  • 打赏
  • 举报
回复
Bitmap bitmap(hInstance, MAKEINTRESOURCEW(IDB_WATERMARK));
然后操作这个bitmap吧
Chendy1985 2008-10-22
  • 打赏
  • 举报
回复
感觉资源转换回去没这么难吧
但是我就是转换不过去
5555555555555
ddszhan 2008-10-22
  • 打赏
  • 举报
回复
原理差不多,把IMAGE资源改成位图的。
BOOL DrawImage(CDC* pDC, UINT nRes, CRect rect)
{
using namespace Gdiplus;

if (nRes==0)
{
return FALSE;
}

HINSTANCE hRess=AfxGetResourceHandle();
if (hRess!=NULL)
{
HRSRC hResInfo=::FindResource(hRess,MAKEINTRESOURCE(nRes),TEXT("IMAGE"));
if (hResInfo!=NULL)
{
HGLOBAL hRes=::LoadResource(hRess,hResInfo);
if (hRes!=NULL)
{
LPVOID pTheImage=::LockResource(hRes);
if (pTheImage!=NULL)
{
DWORD dwTheImage=::SizeofResource(hRess,hResInfo);
ULONG uWrite;
CComPtr<IStream> pStream;
CreateStreamOnHGlobal(NULL,TRUE,&pStream);
pStream->Write(pTheImage,dwTheImage,&uWrite);

{ // 使GDI+的对象生命期提早到达终止
Graphics graphics(pDC->m_hDC);
Image image(pStream);

graphics.DrawImage(&image, rect.left, rect.top, rect.Width(), rect.Height());
}

pStream.Release();
::UnlockResource(hRes);
}
::FreeResource(hRes);
}

}
}
return TRUE;
}
Introduction Like many of my colleagues in this industry, I learned Windows programming from Charles Petzold's Programming Windows—a classic programming text that is the bible to an entire generation of Windows programmers. When I set out to become an MFC programmer in 1994, I went shopping for an MFC equivalent to Programming Windows. After searching in vain for such a book and spending a year learning MFC the old-fashioned way, I decided to write one myself. It's the book you hold in your hands. And it's the book I would like to have had when I was learning to program Windows the MFC way. MFC, as you probably already know, is Microsoft's C++ class library for Windows programming. Programming Windows with MFC isn't a book about C++; rather, it's a book about writing 32-bit Windows applications in C++ using MFC rather than the Windows API as the chief means of accessing the operating system's essential features and services. It was written with two kinds of people in mind: Windows API programmers who want to learn MFC Programmers who have never before programmed Windows Whichever camp you fall into, I assume that you know the C++ programming language already and are comfortable with basic C++ idioms such as derived classes and virtual functions. If these assumptions are true, you're ready to begin climbing the hill that is MFC programming. Even veteran Windows programmers frequently find MFC code confusing the first time they see it, in part because of the presence of code created by the MFC code-generating wizards in Visual C++ and in part because of the countless lines of code hidden away in MFC classes such as CFrameWnd, CDocument, and CView. That's why this book takes a rather unusual approach to teaching MFC. It begins by having you write MFC code by hand (without the wizards) and by utilizing MFC 1.0-style application architectures—that is, applications that use neither documents nor views. Only after you've mastered the fundamentals and become acquainted with basic MFC classes such as CWnd and CWinApp do I introduce the wizards and teach you how to take advantage of MFC's document/view architecture. Along the way, you build a understanding from the ground up of the message-oriented nature of Windows and of key components of Windows itself, such as the Graphics Device Interface (GDI). I believe that this approach makes learning MFC not only less intimidating, but also more enjoyable. I think that you'll agree once you've worked your way through the book and can look back on the learning experience from the standpoint of a knowledgeable Windows programmer. Programming Windows with MFC is divided into four parts. Part I introduces the core tenets of MFC and Windows programming, beginning with a simple "Hello, MFC" application and introducing, one by one, menus, controls, dialog boxes, and other application building blocks. Part II builds on the foundation laid in Part I with a detailed look at the document/view architecture. In particular, Chapters 9, 10, and 11 reveal much of the "magic" behind documents and views and explain not only how to write basic document/view applications but also how to implement some not so basic features such as split-window views of a document and print previews. Part III covers some of the more advanced features of Windows and MFC—features such as color palettes, bitmap handling, and multiple threads of execution. In Part IV, you'll learn how MFC wraps its arms around COM, OLE, and ActiveX and how to write COM-enabled applications and software components. By the time you're finished with Chapter 21, you'll be well versed in the art of 32-bit Windows programming using MFC. And you'll have prodigious amounts of sample code to draw from when it's time to strike out on your own and write your first great Windows application. What's New in the Second Edition Those of you who read the first edition of this book will notice two rather obvious changes in the second edition. First, this edition contains seven new chapters. One is devoted to the MFC view classes; another covers the MFC collection classes; one introduces MFC file I/O and serialization mechanisms; and four cover the relationship between MFC and COM. MFC is not the general-purpose COM framework that the Active Template Library (ATL) is, but MFC makes certain types of COM programming exceptionally easy. For example, MFC greatly simplifies the task of writing ActiveX controls, and it makes writing Automation servers—programs that use COM to expose their functionality to scripting clients—a breeze. The second major change in this edition has to do with wizards. The first edition didn't cover the MFC wizards at all. The second edition uses hand-generated code in Chapters 1 through 3 but then shifts gears and begins using AppWizard and ClassWizard in Chapter 4. Why the change of heart? I still believe that code-generating wizards are an impediment to learning and should be used only by knowledgeable programmers, but I've also come to realize that in the real world, MFC programmers use the wizards. For certain tasks—writing ActiveX controls, for example—it doesn't make sense not to use the wizards. So after much deliberation, I decided I would be remiss not to cover them. Despite the new material regarding wizards, however, this is not—and never will be—a book about clicking buttons in AppWizard. After introducing a fundamental skill, such as how to write a message handler with ClassWizard, I thereafter let the source code do the talking and assume that you can figure out how the source code was created. Keep in mind that the wizards never do anything you can't do yourself, so it's perfectly feasible to type in every source code listing by hand if you'd like to. The downside to using wizards in a book that teaches MFC programming is that they produce code that isn't fit to publish. The first edition of this book included printed listings for each and every source code file. This one does not. It contains printed copies of relevant source code files and provides the others on CD. Why? Because printing a source code file that's 50 percent meat and 50 percent fat adds bulk to a book without adding content. Some of the code produced by the MFC AppWizard in Visual C++ 6.0 won't even compile. (For details, see Chapter 4.) I'm not very proud of the parts of my book that the wizards created, because those portions are littered with arbitrary blank lines, comments that lack consistent style, and unnecessary functions. For someone who takes pride in writing concise, readable sample code, wizard output is a bitter pill to swallow. Nevertheless, wizards represent the new world order in Windows programming, and they're something that you, I, and everyone else must get used to. It's a shame that the Visual C++ team won't give us real wizards to play with instead of the toys that they pass off as wizards today. Until they do, we must make do with what we have. What's On the CD The CD that accompanies this book contains source code and executables for all the sample programs presented in the book. All samples were written and compiled with Visual C++ 6.0 and MFC 6.0 and tested on various Win32 platforms. Unless otherwise noted, all are compatible with Windows 98, Windows NT 4.0, and Windows 2000. Most are also compatible with Windows 95 and Windows NT 3.51. You can copy the contents of the CD to your hard disk by running the setup program found in the CD's root directory, or you can retrieve the files directly from the CD's \Code directory. The \Code directory contains one subdirectory for each chapter of the book—Chap01, Chap02, and so on. Inside these subdirectories you'll find the sample programs. Each set of source code files is accompanied by a release-build EXE as well as a Visual C++ workspace (DSW) file that you can open with Visual C++'s Open Workspace command. From Me to You (and You to Me) From the day in 1995 when I began writing the first edition of Programming Windows with MFC, my goal has been to provide C++ programmers with the same kind of timeless, irreplaceable resource that Programming Windows is to C programmers. Whether I've achieved that goal, I'll let you be the judge. I want to know what you think about Programming Windows with MFC, and I particularly want to hear from you if you find mistakes. You can reach me by sending mail to jeffpro@msn.com or by visiting my Web site at www.prosise.com. At that site you'll find up-to-date information regarding the book, a list of errata, and information about other projects that I'm working on. Later this year, I plan to post a brand new chapter on MFC DLLs that you can read and comment on online. With the huge volume of computer books vying for buyers' attention in bookstores today, I know that you could have chosen any number of MFC books besides this one. I thank you for purchasing Programming Windows with MFC, and I sincerely hope you conclude that your money was well spent. Enjoy! Jeff Prosise

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧