25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScreenshotExtensions.cs 1.7 KiB

8 달 전
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Drawing;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. namespace RenderHookAPI.Interface
  5. {
  6. public static class ScreenshotExtensions
  7. {
  8. public static Bitmap ToBitmap(this byte[] data, int width, int height, int stride, System.Drawing.Imaging.PixelFormat pixelFormat)
  9. {
  10. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  11. try
  12. {
  13. var img = new Bitmap(width, height, stride, pixelFormat, handle.AddrOfPinnedObject());
  14. return img;
  15. }
  16. finally
  17. {
  18. if (handle.IsAllocated)
  19. handle.Free();
  20. }
  21. }
  22. public static Bitmap ToBitmap(this Screenshot screenshot)
  23. {
  24. if (screenshot.Format == ImageFormat.PixelData)
  25. {
  26. return screenshot.Data.ToBitmap(screenshot.Width, screenshot.Height, screenshot.Stride, screenshot.PixelFormat);
  27. }
  28. else
  29. {
  30. return screenshot.Data.ToBitmap();
  31. }
  32. }
  33. public static Bitmap ToBitmap(this byte[] imageBytes)
  34. {
  35. MemoryStream ms = new MemoryStream(imageBytes);
  36. try
  37. {
  38. return (Bitmap)Image.FromStream(ms);
  39. }
  40. catch
  41. {
  42. return null;
  43. }
  44. }
  45. public static byte[] ToByteArray(this Image img, System.Drawing.Imaging.ImageFormat format)
  46. {
  47. using (MemoryStream stream = new MemoryStream())
  48. {
  49. img.Save(stream, format);
  50. stream.Close();
  51. return stream.ToArray();
  52. }
  53. }
  54. }
  55. }