Menü Schließen

Create an Icon from an Image

The following C# code changes an image object into an icon.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public static Icon ConvertImageToIcon(Image img)
{
if (img == null)
return null;
Bitmap square = new Bitmap(img.Width, img.Height); // create new bitmap
Graphics g = Graphics.FromImage(square); // allow drawing to it
g.DrawImage(img, 0, 0, img.Width, img.Height); // draw image with specified dimensions
g.Flush(); // make sure all drawing operations complete before we get the icon
return Icon.FromHandle(square.GetHicon());
}
public static Icon ConvertImageToIcon(Image img) { if (img == null) return null; Bitmap square = new Bitmap(img.Width, img.Height); // create new bitmap Graphics g = Graphics.FromImage(square); // allow drawing to it g.DrawImage(img, 0, 0, img.Width, img.Height); // draw image with specified dimensions g.Flush(); // make sure all drawing operations complete before we get the icon return Icon.FromHandle(square.GetHicon()); }
public static Icon ConvertImageToIcon(Image img)
{
  if (img == null)
    return null;
  Bitmap square = new Bitmap(img.Width, img.Height); // create new bitmap
  Graphics g = Graphics.FromImage(square);           // allow drawing to it
  g.DrawImage(img, 0, 0, img.Width, img.Height);     // draw image with specified dimensions
  g.Flush();                                         // make sure all drawing operations complete before we get the icon
  return Icon.FromHandle(square.GetHicon());
}

 

Ähnliche Beiträge

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert