我在Vc6.0下使用GDI+ 来显示,改变各种形式的图片(主要是BMP,JPEG)都实现了,可是为什么保存功能就不能实现呢执行这句话不成功
int k = m_pImage.Save(A2CW(picname),&pngClsid,NULL);结果返回 k=10,错误类型是 
FileNotFound :Indicates that the specified image file or metafile cannot be found. 
可是图片能显示出来,图片文件没有找到是什么意思呢,picname 是我指定的存放图片的路径名 ,m_pImage是Image类型的变量各位大侠帮忙看看吧
  我还有个问题就是:因为我是基于对话框的程序,我是在CMyStatic控件上显示图片,(CMyStatic 是从CStatic派生而来),对话框最小化以后再打开图片就不见了,我在CMyStatic的OnPaint里重新用显示图片结果就会占用大量的CUP导致程序无法正常运行下去,这是怎么回事呢

解决方案 »

  1.   

    从你给出的代码也看不出问题在哪里,给你贴个代码吧
    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 -1;  // Failure   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
       if(pImageCodecInfo == NULL)
          return -1;  // Failure   GetImageEncoders(num, size, pImageCodecInfo);   for(UINT j = 0; j < num; ++j)
       {
          if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
          {
             *pClsid = pImageCodecInfo[j].Clsid;
             free(pImageCodecInfo);
             return j;  // Success
          }    
       }   free(pImageCodecInfo);
       return -1;  // Failure
    }// 保存图片代码
        CA2W wDesFilePath(DesFilePath);

        CLSID Clsid;
        GetEncoderClsid(L"image/bmp", &Clsid);
        pImage->Save(wDesFilePath, &Clsid, NULL);
    第二个问题,可能是在OnPaint里没有构造CPaintDC或调用了父类的OnPaint,CPaintDC在构造函数里调用了::BeginPaint在析构函数里调用了::EndPaint,这两个函数没有成对出现或混乱可能会引起CPU占用高,注意只是可能是这样。
      

  2.   

    我在单文档程序中做了个测试程序,结果错误类型还是FileNotFound :Indicates that the specified image file or metafile cannot be found.  
    以下是源码:
    void CDView::OnPic() 
    {
    // TODO: Add your command handler code here
    static char BASED_CODE szFilter[] = "BMP Files(*.bmp)|*.bmp|JPEG Files(*.jpeg)|*.jpeg";
    CFileDialog FileDlg (TRUE,"bmp",NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST,szFilter);
    if (IDOK == FileDlg.DoModal())
    {
    CString path;
    path = FileDlg.GetPathName();
    if (!path.IsEmpty())
    {
    USES_CONVERSION;
    Image m_pImage(A2CW(path));
    CDC* pDC = GetDC();
    Graphics m_Graphic(pDC->m_hDC);
    m_Graphic.DrawImage(&m_pImage,Point(10,10));
    CLSID m_clsid;
    m_pImage.GetRawFormat(&m_clsid);//这样得到压缩类型我跟踪了以下,是正确的
    int k = m_pImage.Save(L"fdm.bmp",&m_clsid,NULL);//我是打开一个BMP图片进行测试,
                                                                            //再将它保存为另外的一个名字fdm.bmp
    CString s;
    s.Format("k is: %d",k);
    MessageBox(s); }
    }
    }
      我就是不知道错误类型说是指定的图片文件不存在是什么意思,是m_pImage不存在呢,还是L“fdm.bmp”不存在。执行完后,倒是创建了一个名字为fdm.bmp的文件,可是大小是0;到底怎么回事呢?
      

  3.   

     to SoftUI :谢谢你的帮助,我第一个问题有进展了,可以保存图片了,但是当我改变了图片的大小再保存,保存后的图片还是原来图片的大小。我是利用橡皮筋类改变控件的大小,然后再将图片画到改变了大小的控件上,让图片的大小和控件相同,为什么保存图片后还是原来的大小呢?
    我第二个问题还是没有解决,不知道为什么給控件加上OnPaint函数后,CPU占用率很大,一下是源代码,帮忙看看吧,非常非常感谢!!!
    void CMyStatic::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting

      InvalidateRect(NULL);
      CRect r;
      GetWindowRect(&r);
      if(this->path!="")//path 是图片的路径,我是动态插入图片,创建一个CMyStatic控件,图片画在上面,
                                  //图片的路径保存在CMyStatic的数据成员path中
      { USES_CONVERSION;
           Image m_pImage(A2CW(path));
     
      Graphics m_Graphic(dc);
      Point destinationPoints[] = {
                    Point(0,0),   // destination for upper-left point of original
                    Point(r.Width(), 0),  // destination for upper-right point of original
                    Point(0, r.Height())};  // destination for lower-left point of original       m_Graphic.DrawImage(&m_pImage,destinationPoints,3);
      } // Do not call CStatic::OnPaint() for painting messages
    }
      

  4.   

    你是做缩略图吧试试这个Image *image=NULL;//建立Image指针对象用于加载图片
    image=Image::FromFile(strjpg,true
    Image* pThumbnail = image->GetThumbnailImage(180/*X坐标大小*/, 150/*Y坐标大小*/, NULL, NULL);//GetThumbnailImage是实现缩略图功能
    graphics.DrawImage(pThumbnail, 0, 0, pThumbnail->GetWidth(), pThumbnail->GetHeight());