在Winforms中绘制CachedBitmap

时间:2014-02-23 16:01:46

标签: c# winforms bitmap

我尝试在Winforms中显示缓存的位图(出于性能原因)。我有一个问题因为我无法画它。 这个答案中的摘要https://stackoverflow.com/a/6474581/1676819说应该有类似的东西 graphics.DrawCachedBitmap(bitmap, 0, 0); 我找不到。

到目前为止我做了什么:

  • 我添加了Presentationcore.dll作为参考
  • 我创建了CachedBitmap

CachedBitmap tempCBm = new CachedBitmap(new BitmapImage(new Uri(@"D:\test.bmp")),BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

  • 我试图用标准方法绘制它(导致错误)

    private void CustomPaint(object sender, PaintEventArgs e)
    {
            e.Graphics.DrawImage(tempCBm, 0,0);//error
    }
    

有人可以告诉我,我做错了什么吗?非常感谢提前。

2 个答案:

答案 0 :(得分:3)

CachedBitmap无法通过.NET获得。这是GDI +的一个特色。请改用Bitmap。如果您需要针对性能进行优化,则可以使用unsafe context in C# for faster bitmap access

这里有一个很好的教程:http://www.codeproject.com/Tips/240428/Work-with-bitmap-faster-with-Csharp

答案 1 :(得分:1)

可以使用缓存的位图 - 但由于某种原因,它在标准C#api中不可用。但是你可以绕过它 - 创建一个托管的C ++库来封装你想要暴露给C#的方法。

请参阅我的github回购 - https://github.com/svejdo1/CachedBitmap

用于公开缓存位图的C ++实用程序类

#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>

using namespace Gdiplus;

namespace CachedBitmapUtility {
    public ref class BitmapUtility {
        public:
            static void* CreateCachedBitmapPtr(void* bitmapPtr, void* graphicsHdc) {
                Graphics graphics((HDC)graphicsHdc);
                CachedBitmap* result = new CachedBitmap((Bitmap*)bitmapPtr, &graphics);
                return result;
            }
            static void DisposeCachedBitmap(void* cachedBitmapPtr) {
                delete (CachedBitmap*)cachedBitmapPtr;
            }
            static void DisposeBitmap(void* bitmapPtr) {
                delete (Bitmap*)bitmapPtr;
            }
            static void* GetBitmapPtrFromHICON(void* hicon) {
                return (void*)Bitmap::FromHICON((HICON)hicon);
            }
            static void DrawCachedBitmap(void* hdc, void* cachedBitmapPtr, int x, int y) {
                Graphics graphics((HDC)hdc);
                graphics.DrawCachedBitmap((CachedBitmap*)cachedBitmapPtr, x, y);

            }
    };
}

WinForm应用程序的示例用法:

public partial class MainForm : Form {
  IntPtr m_BitmapPtr;
  IntPtr m_CachedBitmapPtr = IntPtr.Zero;

  public MainForm() {
    InitializeComponent();

    Bitmap bitmap;
    using (var stream = typeof(MainForm).Assembly.GetManifestResourceStream("FormApplication.character.png")) {
      bitmap = (Bitmap)Bitmap.FromStream(stream);
    }
    unsafe {
      m_BitmapPtr = (IntPtr)BitmapUtility.GetBitmapPtrFromHICON((void*)bitmap.GetHicon());
    }
  }

  protected override void OnClosed(EventArgs e) {
    // TODO: refactor - dispose should happen in Dispose event
    unsafe {
      BitmapUtility.DisposeBitmap((void*)m_BitmapPtr);
      BitmapUtility.DisposeCachedBitmap((void*)m_CachedBitmapPtr);
    }
  }

  protected override void OnPaint(PaintEventArgs e) {
    var graphics = e.Graphics;
    IntPtr hdc;
    if (m_CachedBitmapPtr == IntPtr.Zero) {
      hdc = graphics.GetHdc();
      unsafe {
        m_CachedBitmapPtr = (IntPtr)BitmapUtility.CreateCachedBitmapPtr((void*)m_BitmapPtr, (void*)hdc);
      }
      graphics.ReleaseHdc(hdc);
    }

    hdc = graphics.GetHdc();
    unsafe {
      BitmapUtility.DrawCachedBitmap((void*)hdc, (void*)m_CachedBitmapPtr, 0, 0);
    }
    graphics.ReleaseHdc(hdc);
  }
}