Markiert: C#

0

Change String into a Valid File Name

Not all possible characters are suitable for file names. For example path separators cannot be used for file names. This changes any string into a valid file name by replacing the invalid characters with...

0

Convert Image to Byte Array (and back)

The following functions can be used to convert an image object to a byte array (and vise versa). This is often used to store images in a database or file or to use them...

0

Check if Current User has Admin Rights and Restart Application with Elevated Rights

The following code snippet checks if the current user as administrator rights: /// <summary> /// Checks if user has administrator rights /// </summary> public static bool UserIsAdmin() {   WindowsIdentity identity = WindowsIdentity.GetCurrent();   WindowsPrincipal principal = new WindowsPrincipal(identity);   return principal.IsInRole(WindowsBuiltInRole.Administrator); } If the application requires administrator rights for an operation, the following function can...

0

Create an Icon from an Image

The following C# code changes an image object into an icon. public static Icon ConvertImageToIcon(Image img) { if (img == null) return null; Bitmap square = new Bitmap(img.Width, img.Height); // create new bitmap Graphics...

0

Changing size of Images

The following function changes the size of an image object and optionally changes the ratio of the image or crops the image. Perfectly for creating thumbnails. /// <summary> /// Changes the size of an image. /// </summary> /// <param name=“image“>The image.</param> /// <param name=“newWidth“>The new width of the image.</param> /// <param name=“newHeight“>The new height of the image.</param> /// <param name=“resizeOptions“>Options that define how to resize the image.</param> /// <param name=“qualityOptions“>Options that define the rendering quality.</param>...