question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement CachedBitmap functionality in System.Drawing

See original GitHub issue

Background

Drawing a Bitmap in GDI+ has relatively poor performance, since the image is stored in a device-independent format. The CachedBitmap class stores an image in a format that is optimized for display on a particular device. Thus, rendering an image stored in a CachedBitmap is fast, because no processing time is spent converting the image to the format required by the display device. Naturally, such a capability can substantially improve graphics performance in many scenarios. However, for whatever reason, it’s not directly usable from the System.Drawing APIs.

API Proposal

This API is derived from this page of the GDI+ flat API. (I don’t know what GdipEmfToWmfBits is doing on that page, but System.Drawing doesn’t support saving metafiles anyway.)

namespace System.Drawing.Imaging
{
+   public sealed class CachedBitmap : MarshalByRefObject, IDisposable
+   {
+       public CachedBitmap(Bitmap bitmap, Graphics graphics);
+       public void Dispose();  
+   }
} 

namespace System.Drawing
{
    public sealed class Graphics : MarshalByRefObject, IDisposable, IDeviceContext
    {
+       public void DrawCachedBitmap(CachedBitmap cachedBitmap, int x, int y);	 
    }
}

As an aside: currently, libgdiplus doesn’t implement this functionality. I imagine it would be fine to just PNSE when not on Windows for now (or, alternatively, making it “lie” about being a CachedBitmap, and simply calling DrawImage from DrawCachedBitmap).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
saferncommented, Mar 14, 2023

Btw, I set the milestone to future as there’s no rush to implement this, but if we’re able to get it in the next couple of weeks and make it into 5.0, great 😄

1reaction
saferncommented, Jun 25, 2020

it looks like libgdiplus exposes the version with GetLibgdiplusVersion(), so we can gracefully fail when CachedBitmap is not supported.

GetLibgdiplusVersion is not supported in some old versions of libgdiplus so we should consider that when we implement that and trhowing if the version doesn’t support it or if we can’t get the version at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implement CachedBitmap functionality in System.Drawing
The CachedBitmap class stores an image in a format that is optimized for display on a particular device. Thus, rendering an image stored...
Read more >
c# - Drawing a CachedBitmap in Winforms
CachedBitmap is not available through .NET. It is a feature of GDI+. Use Bitmap instead. If you need to optimize for performance, ...
Read more >
CachedBitmap Class (System.Windows.Media.Imaging)
Initializes a new instance of CachedBitmap that has the specified source, bitmap create options, and bitmap cache option. Properties. CanFreeze. Gets a value ......
Read more >
CachedBitmap Class (System.Drawing.Imaging)
Determines whether the specified object is equal to the current object. (Inherited from Object). Finalize() · GetHashCode(). Serves as the default hash function...
Read more >
How do you use System.Drawing in .NET Core?
Drawing.Common to provide access to GDI+ graphics functionality cross-platform. There is a lot of existing code - mine included - that makes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found