a505615911 2013-09-25 09:03 采纳率: 0%
浏览 1271

MFC dc显示超大图片 ,但比屏幕小的图片显示不出来

CRect rect;
    CImage image;
    GetWindowRect(&rect);
    ScreenToClient(&rect);
    image.Load(m_strRollPhotos[m_stCurrentPhoto].c_str());

    int nImgWidth = image.GetWidth();
    int nImgHeight =image.GetHeight();
    if(nImgHeight==0)return;
    CBitmap bitmap;
    bitmap.Attach(image.Detach());

    CClientDC dc(this);

    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    CBitmap* pbmpOld = memDC.SelectObject(&bitmap);

    dc.SetStretchBltMode(COLORONCOLOR);//设置拉伸模式,否则图像会不清晰
    dc.StretchBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0, nImgWidth, nImgHeight, SRCCOPY);//绘制图像
    memDC.SelectObject(pbmpOld);

    memDC.DeleteDC();

由于我要显示大图片,像素差不多10000*10000的,所以用这种方法显示 ,但是用这种方法后比屏幕小的图片就显示不出来了

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-02-20 12:18
    关注

    该回答引用ChatGPT

    根据您提供的代码,您正在使用MFC绘制图像,但在比屏幕小的图像上无法正确显示。这可能是由于您将图像拉伸到与窗口大小相同,而比屏幕小的图像可能被拉伸得太大而超出了窗口大小,因此无法显示出来。


    为了解决这个问题,您可以考虑在绘制图像之前检查图像的大小,并根据需要进行缩放。可以通过比较图像的大小和窗口的大小来确定是否需要进行缩放。如果图像的大小比窗口大,则可以将其缩小到适当的大小。如果图像的大小比窗口小,则可以将其保持原样或者在图像周围添加填充以适应窗口。


    以下是一个可能的实现示例:

    CRect rect;
    CImage image;
    GetWindowRect(&rect);
    ScreenToClient(&rect);
    image.Load(m_strRollPhotos[m_stCurrentPhoto].c_str());
    
    int nImgWidth = image.GetWidth();
    int nImgHeight = image.GetHeight();
    if (nImgHeight == 0) return;
    
    // Determine the scale factor based on the image and window sizes
    float fScaleX = static_cast<float>(rect.Width()) / static_cast<float>(nImgWidth);
    float fScaleY = static_cast<float>(rect.Height()) / static_cast<float>(nImgHeight);
    
    // Calculate the scaled image size
    int nScaledImgWidth = nImgWidth;
    int nScaledImgHeight = nImgHeight;
    if (fScaleX < 1.0f || fScaleY < 1.0f) {
        float fScale = min(fScaleX, fScaleY);
        nScaledImgWidth = static_cast<int>(nImgWidth * fScale);
        nScaledImgHeight = static_cast<int>(nImgHeight * fScale);
    }
    
    // Create a compatible DC and bitmap for the scaled image
    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    CBitmap bitmap;
    bitmap.CreateCompatibleBitmap(&dc, nScaledImgWidth, nScaledImgHeight);
    CBitmap* pbmpOld = memDC.SelectObject(&bitmap);
    
    // Draw the scaled image
    image.StretchBlt(memDC.GetSafeHdc(), 0, 0, nScaledImgWidth, nScaledImgHeight, SRCCOPY);
    
    // Determine the position and size of the scaled image within the window
    CRect rectImage;
    rectImage.left = (rect.Width() - nScaledImgWidth) / 2;
    rectImage.top = (rect.Height() - nScaledImgHeight) / 2;
    rectImage.right = rectImage.left + nScaledImgWidth;
    rectImage.bottom = rectImage.top + nScaledImgHeight;
    
    // Draw the scaled image in the window
    dc.SetStretchBltMode(COLORONCOLOR);
    dc.StretchBlt(rectImage.left, rectImage.top, rectImage.Width(), rectImage.Height(), &memDC, 0, 0, nScaledImgWidth, nScaledImgHeight, SRCCOPY);
    
    // Clean up
    memDC.SelectObject(pbmpOld);
    bitmap.DeleteObject();
    memDC.DeleteDC();
    

    这个示例代码会首先确定需要缩放图像的大小,然后创建一个兼容的DC和位图来绘制缩放后的图像。最后,它会将缩放后的图像绘制在窗口中心,并在窗口中绘制缩放后的图像。这个方法可以确保无论图像的大小是多少,都可以适当地缩放并在窗口中居中显示。


    另外,由于您要显示非常大的图像,可能需要考虑使用分块加载的技术来避免在加载整个图像时出现内存不足的问题。例如,您可以按照图像的大小将其分成多个较小的块,并在需要时仅加载和显示当前可见块。这种方法可以帮助您在处理大型图像时提高性能和效率。


    最后,请注意,使用内存占用较大的图像可能会导致内存不足或性能问题。因此,建议使用适当的压缩格式来减小图像的大小,或者使用其他技术来优化内存和性能。

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题