Imaging on C#

/ May 27, 2020/ Programming/ 0 comments

There are many possibilities to process graphical information in .Net.image
Microsoft has Graphics Device Interface for imaging. Its functionality is provided by System.Drawing.Common namespace in .Net Core and Framework.
By means of C# imaging it is possible:

  • to create images
  • to edit images
  • to create 2D animations
  • to create math graphics
  • to draw formatted text

Intro of imaging on C#

Adding imaging namespaces

First of all, System.Drawing.Common should be installed. It is downloadable from nuget.

using System.Drawing;
using System.Drawing.Imaging;
Some classes
  • Image – an abstract class that provides basic functionality of GDI
  • Bitmap – inherits Image, represents pixel image
  • Graphics – inherits Image, represent drawing surface
Load image from file and save to file
...
   Image image = Image.FromFile("path");  //constructor with string parameter (path to file)

   image.Save("otherpath");  // saving image
...
Codec
mime typeextension
image/apng.apng
image/bmp.bmp
image/jpeg.jpg, .jpeg
image/png.png
image/webp.webp

System.Drawing.Common only works with Bitmap images. When image is opened GDI uses decoder to transform it to Bitmap. When image is saved GDI uses encoder to prepare image to be saved with a needed extension. Encoder and decoder are called Codec. If Codec is not specified the default one will be used.

Method for getting encoder

...
   public static ImageCodecInfo GetEncoder(string mimetype)
   {
      return ImageCodecInfo.GetImageEncoders().FirstOrDefault(e => e.MimeType == mimetype);
   }
...

Method for getting decoder

...
   public static ImageCodecInfo GetDecoder(string mimetype)
   {
      return ImageCodecInfo.GetImageDecoders().FirstOrDefault(e => e.MimeType == mimetype);
   }
...

How to cut picture from image on C#

Method for cutting Rectangle (at point (x,y), with height h and width w) from Image.

...
   public static Image Cut(int x, int y, int w, int h, Image image)
   {
      Rectangle rect = new Rectangle(x, y, w, h);

      using Bitmap target = new Bitmap(w, h);

      using (Graphics g = Graphics.FromImage(target))
      {
         g.DrawImage(image, 0, 0, rect, GraphicsUnit.Pixel);
      }

      image = new Bitmap(target, new Size(target.Width, target.Height));

      return image;
   }
...

How to save image with a specific number of bytes

Method for changing image in order to make it consist of needed number of bytes.
It reduces image quality until its size is less than parameter value (memoryInKb) and then saves it to provided path.

...
   public static void Save(string fullpath, int memoryInKb, Image image)
   {
      long qual = 101;

      ImageCodecInfo imagecodecinfo = GetEncoder("image/jpeg");  //GetEncoder is set earlier

      System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;

      using EncoderParameters encoderparams = new EncoderParameters(1);
      double current_size=0;
      do
      {
         qual--;
         EncoderParameter encoderparam = new EncoderParameter(encoder, qual);
         encoderparams.Param[0] = encoderparam;
         
         using MemoryStream mem = new MemoryStream();
         image.Save(mem, imagecodecinfo, encoderparams);
         string text = Convert.ToBase64String(mem.ToArray());
         current_size = Encoding.Default.GetByteCount(text) / 1.333 / 1024;
      }
      while (current_size>memoryInKb);

      image.Save(fullpath, imagecodecinfo, encoderparams);
   }
...

How to change image extension on c#

...
   public static void ChangeExtension(string originpath,string destinationpath, ImageFormat format)
   {
     using Image image = Image.FromFile(originpath);

     image.Save(destinationpath, format);
   }
...

How to resize image on c#

Method that returns resized image with a spesific size.

...
   public static Image Resize(Size size, Image image)
   {
     return new Bitmap(image, size);
   }
...

How to scale image on c#

Method that proportionally resizes image. It uses the previous method Resize.

...
   public static Image Scale(bool relation, int size, Image image)
   {
      double A = image.Width, B = image.Height;
      if (relation)
      {
         image = Resize(new Size(size, (int)(size * B / A)), image);
      }
      else
      {
         image = Resize(new Size((int)(size / B * A), size), image);
      }
      return image;
   }
...

How to draw rectangle over image using c#

...
   public static Image DrawRect(Image image, int x, int y, int w,int h)
   {
      using Pen pen = new Pen(Color.Black, 1);
      Bitmap bitmap = new Bitmap(image);
      Graphics graphics = Graphics.FromImage(bitmap);
      graphics.DrawRectangle(pen, x, y, w, h);
      return bitmap;
   }
...
Share this Post

Leave a Comment