Bitmap Image Utils : Bitmap « 2D Graphics « C# / C Sharp






Bitmap Image Utils

       

using System;
#if !SILVERLIGHT
using System.Drawing;
#endif
using System.IO;
using System.Windows.Media.Imaging;
using Color = System.Windows.Media.Color;

namespace FreeGoo.WpfRenderer
{
    public static class BitmapImageUtils
    {
#if SILVERLIGHT
        public static BitmapImage LoadBitmapImage(string imageName)
        {            
            BitmapImage bi = new BitmapImage();
            bi.UriSource = new Uri(new Uri(Directory.GetCurrentDirectory()+"/"), imageName);
            return bi;
        }

        public static BitmapImage LoadAndColorizeBitmapImage(string imageName, Color color)
        {
            return LoadBitmapImage(imageName);
        }
#else                
        public static BitmapImage LoadBitmapImage(string imageName)
        {           
            FileStream fs = File.Open(imageName, FileMode.Open);
            Bitmap bitmap = new Bitmap(fs);
            return Convert(bitmap);
        }

        public static BitmapImage LoadAndColorizeBitmapImage(string imageName, Color color)
        {
            FileStream fs = File.Open(imageName, FileMode.Open);
            using (fs)
            {
                Bitmap bitmap = new Bitmap(fs);
                ColorizeBitmap(bitmap, color);
                return Convert(bitmap);
            }
        }        

        public static Color Colorize(Color originalColor, Color color)
        {
            int strength = (originalColor.R + originalColor.G + originalColor.B);
            return
                Color.FromArgb(
                    originalColor.A,
                    System.Convert.ToByte(color.R * strength / 255 / 3),
                    System.Convert.ToByte(color.G * strength / 255 / 3),
                    System.Convert.ToByte(color.B * strength / 255 / 3));
        }        

        public static Color ToMediaColor(this System.Drawing.Color color)
        {
            return
                Color.FromArgb(color.A, color.R, color.G, color.B);
        }

        public static System.Drawing.Color ToSystemColor(this Color color)
        {
            return
                System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
        }
        
        public static BitmapImage Convert(Bitmap bitmap)
         {
             MemoryStream memoryStream = new MemoryStream();
             bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
             BitmapImage bitmapImage = new BitmapImage();

             bitmapImage.BeginInit();
             bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
             bitmapImage.EndInit();
             return bitmapImage;
         }

        public static Bitmap Convert(BitmapImage bitmap)
        {
            MemoryStream memoryStream = new MemoryStream();
            BitmapEncoder bitmapEncoder = new BmpBitmapEncoder();
            bitmapEncoder.Frames.Add(BitmapFrame.Create(memoryStream));
            bitmapEncoder.Save(memoryStream);
            return new Bitmap(memoryStream);
        }

        public static void ColorizeBitmap(Bitmap bitmap, Color tintColor)
        {
            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    Color clearPixel = bitmap.GetPixel(x, y).ToMediaColor();
                    Color tintedPixel = Colorize(clearPixel, tintColor);

                    bitmap.SetPixel(x, y, tintedPixel.ToSystemColor());
                }
            }
        }
#endif
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Draw on an Bitmap
2.Resize the Bitmap using the lowest quality interpolation mode
3.Resize the Bitmap using the highest quality interpolation mode
4.Create Graphics object From Image
5.Bitmap.SetResolution
6.Bitmap property: Height, Physical Dimension, width, raw format and sizeBitmap property: Height, Physical Dimension, width, raw format and size
7.Create your own BitMapCreate your own BitMap
8.Draw shapes to the bitmap in memoryDraw shapes to the bitmap in memory
9.new Bitmap(bitmap, size)
10.Read Bitmap Size by using BinaryReader
11.Bitmap.HorizontalResolution
12.Use a color matrix to change the color properties of the image
13.Create a Bitmap image in memory and set its CompositingMode
14.Create a red color with an alpha component then draw a red circle to the bitmap in memory
15.Create a green color with an alpha component then draw a green rectangle to the bitmap in memory
16.write the pixel information to the console window
17.Double buffer with Bitmap
18.Draw an array of imagesDraw an array of images
19.Bit operation with PixelFormat.Alpha
20.PixelFormat.DontCare
21.Scale Bitmap By Percent
22.Draws Bitmap in a cell within a DataGridView
23.Bitmap Operations
24.Make Bitmap from UIElement
25.Creates a new bitmap from a specific region of another bitmap
26.Allows drawing fonts with borders and auto centers the font on a bitmap.
27.Create New Bitmap From Image
28.Simple Resize Bmp
29.Create Thumbnail
30.Calculate the RBG projection
31.Make Thumb