View转化为bitmap

本文详细介绍了如何从Android View组件中获取Bitmap图像,并在操作过程中优化内存使用。
private Bitmap getViewBitmap(View v) {   
        v.clearFocus();   
        v.setPressed(false);   
   
        boolean willNotCache = v.willNotCacheDrawing();   
        v.setWillNotCacheDrawing(false);   
   
        // Reset the drawing cache background color to fully transparent   
        // for the duration of this operation   
        int color = v.getDrawingCacheBackgroundColor();   
        v.setDrawingCacheBackgroundColor(0);   
   
        if (color != 0) {   
            v.destroyDrawingCache();   
        }   
        v.buildDrawingCache();   
        Bitmap cacheBitmap = v.getDrawingCache();   
        if (cacheBitmap == null) {   
            Log.e("TTTTTTTTActivity", "failed getViewBitmap(" + v + ")", new RuntimeException());   
            return null;   
        }   
   
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);   
   
        // Restore the view   
        v.destroyDrawingCache();   
        v.setWillNotCacheDrawing(willNotCache);   
        v.setDrawingCacheBackgroundColor(color);   
   
        return bitmap;   
    }  

  

转载于:https://www.cnblogs.com/qgli/p/3173556.html

### 将 QGraphicsView 转换为 HDC 的实现方法 在 Qt 中,`QGraphicsView` 是用于显示 `QGraphicsScene` 的视图组件。为了将其转换为 Windows 平台上的设备上下文 (HDC),可以采用以下方式: #### 方法概述 可以通过将 `QGraphicsView` 渘存到一个临时的图像对象 (`QImage`) 中,再利用该图像创建 GDI 对象并获取其对应的 HDC。 以下是具体实现过程: 1. **渲染 QGraphicsView 到 QImage**: 使用 `QGraphicsView::render()` 函数将视图的内容绘制到一个 `QImage` 上。 2. **从 QImage 获取 HBITMAP 和 HDC**: 创建一个与 `QImage` 兼容的位图句柄,并通过该位图获得设备上下文 (HDC)。 下面是完整的代码示例: ```cpp #include <QApplication> #include <QGraphicsView> #include <QGraphicsScene> #include <QImage> #include <windows.h> HDC convertQGraphicsViewToHDC(QGraphicsView* view) { // Step 1: Create a QImage with the same size as the QGraphicsView. QSize viewSize = view->size(); QImage image(viewSize, QImage::Format_ARGB32); image.fill(Qt::transparent); // Step 2: Render the QGraphicsView content into the QImage. QPainter painter(&image); view->render(&painter); // Render the QGraphicsView to the QImage. // Step 3: Convert QImage to HBITMAP and get its HDC. HBITMAP hBitmap = nullptr; BITMAPINFO bmi = {0}; bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = image.width(); bmi.bmiHeader.biHeight = -(LONG)image.height(); // Top-down DIB bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; void* bits = nullptr; hBitmap = CreateDIBSection(nullptr, &bmi, DIB_RGB_COLORS, &bits, nullptr, 0); if (!hBitmap || !bits) { DeleteObject(hBitmap); return nullptr; } memcpy(bits, image.bits(), static_cast<size_t>(image.byteCount())); // Get the device context of the bitmap. HDC hdc = CreateCompatibleDC(nullptr); SelectObject(hdc, hBitmap); return hdc; // Return the HDC associated with the rendered QGraphicsView. } int main(int argc, char *argv[]) { QApplication app(argc, argv); QGraphicsScene scene; QGraphicsView view(&scene); // Example usage: HDC hdc = convertQGraphicsViewToHDC(&view); if (hdc != nullptr) { // Use the HDC here... DeleteDC(hdc); // Clean up when done. } return app.exec(); } ``` --- #### 关键点说明 - **QGraphicsView::render()**[^1]: 此函数允许将整个视图内容渲染到目标绘图器(如 `QPainter`),从而能够轻松捕获视图中的所有图形项。 - **CreateDIBSection**[^2]: 这是一个 Windows API 函数,用于创建兼容的内存设备无关位图 (DIB)。它提供了对像素数据的直接访问权限,因此可以直接复制来自 `QImage` 的数据。 - **SelectObject**[^3]: 将新创建的位图选入指定的 DC 中,以便后续操作可以在该 DC 下完成。 - **DeleteDC/Release Resources**[^4]: 确保释放由 `CreateCompatibleDC` 或其他资源管理函数分配的所有资源,以防止内存泄漏。 --- #### 注意事项 - 如果需要跨平台支持,则应考虑替代方案,因为 HDC 特定于 Windows 操作系统。 - 在实际应用中可能还需要处理 DPI 缩放问题以及不同分辨率下的适配情况。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值