BitmapHandle.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.ConstrainedExecution;
  4. using System.Runtime.InteropServices;
  5. using System.Security;
  6. namespace HandyControl.Tools.Interop;
  7. [SuppressMessage("ReSharper", "UnusedMember.Local")]
  8. internal sealed class BitmapHandle : WpfSafeHandle
  9. {
  10. [SecurityCritical]
  11. private BitmapHandle() : this(true)
  12. {
  13. //请不要删除此构造函数,否则当使用自定义ico文件时会报错
  14. }
  15. [SecurityCritical]
  16. private BitmapHandle(bool ownsHandle) : base(ownsHandle, CommonHandles.GDI)
  17. {
  18. }
  19. [SecurityCritical]
  20. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  21. protected override bool ReleaseHandle()
  22. {
  23. return InteropMethods.DeleteObject(handle);
  24. }
  25. [SecurityCritical]
  26. internal HandleRef MakeHandleRef(object obj)
  27. {
  28. return new(obj, handle);
  29. }
  30. [SecurityCritical]
  31. internal static BitmapHandle CreateFromHandle(IntPtr hbitmap, bool ownsHandle = true)
  32. {
  33. return new(ownsHandle)
  34. {
  35. handle = hbitmap,
  36. };
  37. }
  38. }