ImageUtils.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommonFroms
  6. {
  7. public static class ImageUtils
  8. {
  9. public static System.Drawing.Bitmap ImageToBitmap(System.Drawing.Image dimg)
  10. {
  11. System.Drawing.Bitmap image;
  12. if (!(dimg is System.Drawing.Bitmap) || dimg.Palette != null)
  13. {
  14. image = new System.Drawing.Bitmap(dimg.Width, dimg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  15. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
  16. try
  17. {
  18. g.DrawImage(
  19. dimg,
  20. new System.Drawing.Rectangle(0, 0, dimg.Width, dimg.Height),
  21. new System.Drawing.Rectangle(0, 0, dimg.Width, dimg.Height),
  22. System.Drawing.GraphicsUnit.Pixel);
  23. }
  24. finally { g.Dispose(); }
  25. }
  26. else
  27. {
  28. image = (System.Drawing.Bitmap)dimg;
  29. }
  30. return image;
  31. }
  32. }
  33. }