/* =======================================================*
// cl  show_rgb.cpp 
// for rainbow
// img rgb file:
// line1: img_w img_h
// line2...: RRGGBBrrggbbRRGGBB  in %2X%2X%2X and no space
L_o_o_n_i_e  
* ========================================================*/
#include "StdAfx.h"
#include <Afxwin.h>
#include <process.h>
#include <math.h>
#include <Winuser.h>
#include <string>
#include <cstring>
#include <windows.h>
#include <stdio.h>
#define ULONG_PTR ULONG#include "GdiPlus.h"#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;//#include <iostream.h>
#include <iostream>
#define DEBUG 1
#pragma comment (lib, "User32.lib")
using namespace std;
using namespace Gdiplus;  typedef struct 
{
float x;
float y;
}MY_POINT;
typedef struct 
{
int r;
int g;
int b;
}MY_RGB;
typedef struct  
{
MY_POINT pt;
MY_RGB rgb;
}MY_ORDER;struct _GdiplusToken  {  
         GdiplusStartupInput gdiplusStartupInput;  
ULONG_PTR gdiplusToken;  
_GdiplusToken()  
{  
//VERIFY( GdiplusStartup(&gdiplusToken, &gdiplusStar*/tupInput, NULL) == Ok);  
}  
~_GdiplusToken()  
{  
GdiplusShutdown(gdiplusToken);  
}    
}GdiplusToken; HWND hWndMain;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
void OnDisplayMessage(char *str);
char one_line[80],pname[80],namein[80],str[120];
int len,NN;
LPTSTR argv;
FILE *fin;
RECT RectX;
int x_shift=10,y_shift=20;
COLORREF *img;
int img_w,img_h;
unsigned  int r,g,b;MY_ORDER order[86617610];int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{

MSG Msg;
int i,j,k;
if ( (fin = fopen("E:\\left3.txt","r"))==NULL)
{
sprintf(str,"Can not open %s",namein); OnDisplayMessage( str);exit(0);
};
fscanf(fin,"%d %d\n",&img_w,&img_h);
if (DEBUG==1)
{
sprintf(str,"img_w=%d img_h=%d\n",img_w,img_h);
OnDisplayMessage( str);
}
img = (COLORREF *) malloc (sizeof(COLORREF) * img_w * img_h);//存储分配内存空间
if (!img)
{
sprintf(str,"No enough memory foe img"); OnDisplayMessage( str);exit(0);
} MY_POINT point;
MY_RGB rrggbb;

int num=0;
int i_o=0; while(!feof(fin))
{
//if (i_o>=20000) break;
if (num==0)
{
num++;
continue;
}
i_o++;

if (EOF==fscanf(fin,"%f",&point.x))break;       if (EOF==fgetc(fin))break;
if (EOF==fscanf(fin,"%f",&point.y))break;       if (EOF==fgetc(fin))break;
if (EOF==fscanf(fin,"%d",&rrggbb.r))break;       if (EOF==fgetc(fin))break;
if (EOF==fscanf(fin,"%d",&rrggbb.g))break;       if (EOF==fgetc(fin))break;
if (EOF==fscanf(fin,"%d",&rrggbb.b))break;       if (EOF==fgetc(fin))break;

order[i_o-1].pt.x=point.x;
order[i_o-1].pt.y=point.y;
order[i_o-1].rgb.r=rrggbb.r;
order[i_o-1].rgb.g=rrggbb.g;
order[i_o-1].rgb.b=rrggbb.b;
}
fclose(fin); if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;  
ShowWindow(hWndMain,nCmdShow);
UpdateWindow(hWndMain);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)  
{  
UINT  num = 0;          // number of image encoders   
UINT  size = 0;         // size of the image encoder array in bytes    ImageCodecInfo* pImageCodecInfo = NULL;   GetImageEncodersSize(&num, &size);  
if(size == 0)  
return FALSE;  // Failure    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));  
if(pImageCodecInfo == NULL)  
return FALSE;  // Failure    GetImageEncoders(num, size, pImageCodecInfo);   for(UINT j = 0; j < num; ++j)  
{  
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )  
{  
//pClsid=new CLSID;
*pClsid = pImageCodecInfo[j].Clsid;  
free(pImageCodecInfo);  
return TRUE;  // Success   
}      
}   free(pImageCodecInfo);  
return FALSE;  // Failure   
}  BOOL SaveHDCToFile(HDC hDC)  
{     
BOOL bRet = FALSE;  
int nWidth = RectX.right - RectX.left;  
int nHeight = RectX.bottom - RectX.top;   //将目标区域贴图到内存BITMAP   
HDC memDC = CreateCompatibleDC(hDC);   
HBITMAP hBmp = CreateCompatibleBitmap(hDC, nWidth, nHeight);  
SelectObject(memDC, hBmp);  
BitBlt(memDC, RectX.left, RectX.top, nWidth, nHeight,  
hDC, 0, 0, SRCCOPY);   //保存成文件   
{  
//L"image/bmp" L"image/jpeg"  L"image/gif" L"image/tiff" L"image/png"   
CLSID pngClsid;  
GetEncoderClsid(L"image/bmp", &pngClsid);   Gdiplus::Bitmap *pbmSrc = Gdiplus::Bitmap::FromHBITMAP(hBmp, NULL);  
if( pbmSrc->Save(L"E:\\test.bmp", &pngClsid) == Ok)  
{  
bRet = TRUE;  
}  
delete pbmSrc;  
}   //清理工作   
SelectObject(memDC, (HBITMAP)NULL);  
DeleteDC(memDC);    
DeleteObject(hBmp);   return bRet;  
}  
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static long nXChar,nYChar,xInc,yInc;
static int xClient, yClient;      
static int xClientMax;   
static int xChar, yChar;       
//static int xPos,yPos;     
//static int xMax,yMax;     
SCROLLINFO si; 
HDC hdc;
short x;
TEXTMETRIC tm;
PAINTSTRUCT ps;
COLORREF color;
HFONT font;
//HPEN hP1;  // pen
HBRUSH hBr,hBrR,hBrG,hBrB;
//CPoint aP,mousePos;
HWND h_wnd2;

int i,j,k;


switch(message)
{
case WM_CREATE:

hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
nXChar=tm.tmAveCharWidth;
nYChar=tm.tmHeight;
xInc=1;yInc=1; xChar=nXChar; yChar=nYChar;  // for scroll window
ReleaseDC(hwnd,hdc);
xClientMax = 800;
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);        // begin paint
SetGraphicsMode(hdc,GM_ADVANCED); 
SetWindowExtEx(hdc,600,400,NULL);    // cx=700,cy=400 logical unit
SetViewportExtEx(hdc,300,200,NULL);
SetViewportOrgEx(hdc,x_shift,10,NULL);
SetMapMode(hdc,MM_ANISOTROPIC);      

font=CreateFont(
24,10,0,0, FW_NORMAL,0,0,0, ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,NULL,"myfont"
);
SelectObject(hdc,font);
GetTextMetrics(hdc,&tm);
nYChar=tm.tmHeight;

hBr = CreateSolidBrush(RGB(255,255,255)); 
SelectObject(hdc,hBr);
RectX.left= x_shift;  RectX.right=RectX.left +400;
RectX.bottom = y_shift; RectX.top = RectX.bottom + 400;
FillRect(hdc, &RectX, hBr); 
//画点,用矩形填充
k=0; 
for (i=0;i<9000000;i++)
  {
  
hBr = CreateSolidBrush(RGB(order[k].rgb.r,order[k].rgb.g,order[k].rgb.b)); k++;
SelectObject(hdc,hBr);
RectX.left= x_shift +order[k].pt.x;   
RectX.right=RectX.left+ 1;
RectX.bottom = y_shift + order[k].pt.y; 
RectX.top = RectX.bottom + 1;
FillRect(hdc, &RectX, hBr); 
DeleteObject(hBr);

}


EndPaint(hwnd,&ps);    // end paint

return 0L;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
SaveHDCToFile(hdc);
default:
return DefWindowProc(hwnd,message,wParam,lParam); 
}
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,"END");
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName="Windows Fill";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
return(RegisterClass(&wndclass));
}
void OnDisplayMessage(char *str)
{
HWND hWnd;
MessageBox(NULL,str,"show_rgb_info",MB_OK);     // API
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{ HWND hWnd;
hWnd=CreateWindow(  "Windows Fill",  "Show_color",
WS_OVERLAPPEDWINDOW | WS_SYSMENU | WS_BORDER | WS_HSCROLL | WS_VSCROLL,  100,100,800,400, NULL, NULL, hInstance, NULL );
if(!hWnd) return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
出现的问题:。
CreIMG.exe 中的 0x7c939af2 处最可能的异常: 0xC0000005: 写入位置 0x00000010 时发生访问冲突
CreIMG.exe 中的 0x7c939af2 处未处理的异常: 0xC0000005: 写入位置 0x00000010 时发生访问冲突
指向是是 return DllExports::GdipGetImageEncodersSize(numEncoders, size);

解决方案 »

  1.   

    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
    GDI需要初始化,退出程序时要销毁:
    Gdiplus::GdiplusShutdown(m_gdiplusToken);
    头文件:
    ULONG_PTR m_gdiplusToken;
      

  2.   

    老是出现GdiplusStartup问题,是什么原因
      

  3.   

    VC6? GDIPlus的动态库有没有?
    用API吧 刚写了一个API的  
    WINAPI 拷贝指定的hDC的lpRect部分到文件Dstfile中(BMP文件格式)