Create an ImageAttributes object and set its color matrix

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
Bitmap bmp = new Bitmap(“rama.jpg”);
g.FillRectangle(Brushes.White, this.ClientRectangle);

for (int i = 1; i <= 7; ++i) { Rectangle r = new Rectangle(i * 40 - 15, 0, 15, this.ClientRectangle.Height); g.FillRectangle(Brushes.Gray, r); } float[][] matrixItems = { new float[] {1, 0, 0, 03, 0}, new float[] {0, 1, 0, 0.1f, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, 0.6f, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes imageAtt = new ImageAttributes(); imageAtt.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); g.DrawImage( bmp, this.ClientRectangle, // destination rectangle 0.0f, // source rectangle x 0.0f, // source rectangle y bmp.Width, // source rectangle width bmp.Height, // source rectangle height GraphicsUnit.Pixel, imageAtt); imageAtt.Dispose(); } public static void Main() { Application.Run(new Form1()); } } [/csharp]

Get Bytes From BitmapSource

   
 

/*
 License:  Microsoft Public License (Ms-PL)  
 http://c4fdevkit.codeplex.com/license
 C4F Developer Kit

*/
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media.Imaging;

public class MainClass{

        static byte[] GetBytesFromBitmapSource(BitmapSource bmp)
        {
            int width = bmp.PixelWidth;
            int height = bmp.PixelHeight;
            int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);

            byte[] pixels = new byte[height * stride];

            bmp.CopyPixels(pixels, stride, 0);

            return pixels;
        }
}

   
     


Get Bitmap Source

   
 
//http://simpledbbrowser.codeplex.com/
//License:  Microsoft Public License (Ms-PL)  
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace AWS.Framework.WPF.Utility
{
    public sealed class Helpers
    {
        public static BitmapSource GetBitmapSource(FrameworkElement element)
        {
            DrawingVisual visual = new DrawingVisual();
            DrawingContext context = visual.RenderOpen();
            VisualBrush elementBrush = new VisualBrush(element);
            int w = (int)element.ActualWidth;
            int h = (int)element.ActualHeight;
            context.DrawRectangle(elementBrush, null, new Rect(0, 0, w, h));
            context.Close();

            RenderTargetBitmap bitmap = new RenderTargetBitmap(w, h, 96, 96, PixelFormats.Default);
            bitmap.Render(visual);
            return bitmap;
        }
   }
}

   
     


Image Resize

   
 
//http://blazingcms.codeplex.com/
//GNU General Public License version 2 (GPLv2)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;

namespace Blazing.Web.Util
{
    #region ImageResize CLASS
    /// <summary>
    /// Found: http://www.codeproject.com/KB/aspnet/Thumbnail_Images.aspx
    /// 
    /// ImageResize is a class that is based on an article that was obtained from
    /// the URL http://www.devx.com/dotnet/Article/22079/0/page/3. I had to make
    /// some minor changes to a couple of the properties, but otherwise it is very
    /// much like the original article.
    /// </summary>
    public class ImageResize
    {
        #region Instance Fields
        //instance fields
        private double m_width, m_height;
        private bool m_use_aspect = true;
        private bool m_use_percentage = false;
        private System.Drawing.Image m_src_image, m_dst_image;
        private System.Drawing.Image m_image;
        private ImageResize m_cache;
        private Graphics m_graphics;
        #endregion
        #region Public properties
        /// <summary>
        /// gets of sets the File
        /// </summary>
        public System.Drawing.Image File
        {
            get { return m_image; }
            set { m_image = value; }
        }
        /// <summary>
        /// gets of sets the Image
        /// </summary>
        public System.Drawing.Image Image
        {
            get { return m_src_image; }
            set { m_src_image = value; }
        }
        /// <summary>
        /// gets of sets the PreserveAspectRatio
        /// </summary>
        public bool PreserveAspectRatio
        {
            get { return m_use_aspect; }
            set { m_use_aspect = value; }
        }
        /// <summary>
        /// gets of sets the UsePercentages
        /// </summary>
        public bool UsePercentages
        {
            get { return m_use_percentage; }
            set { m_use_percentage = value; }
        }
        /// <summary>
        /// gets of sets the Width
        /// </summary>
        public double Width
        {
            get { return m_width; }
            set { m_width = value; }
        }
        /// <summary>
        /// gets of sets the Height
        /// </summary>
        public double Height
        {
            get { return m_height; }
            set { m_height = value; }
        }
        #endregion
        #region Public Methods
        /// <summary>
        /// Returns a Image which represents a rezised Image
        /// </summary>
        /// <returns>A Image which represents a rezised Image, using the 
        /// proprerty settings provided</returns>
        public virtual System.Drawing.Image GetThumbnail()
        {
            // Flag whether a new image is required
            bool recalculate = false;
            double new_width = Width;
            double new_height = Height;
            // Load via stream rather than Image.FromFile to release the file
            // handle immediately
            if (m_src_image != null)
                m_src_image.Dispose();
            m_src_image = m_image;
            recalculate = true;
            // If you opted to specify width and height as percentages of the original
            // image&#039;s width and height, compute these now
            if (UsePercentages)
            {
                if (Width != 0)
                {
                    new_width = (double)m_src_image.Width * Width / 100;

                    if (PreserveAspectRatio)
                    {
                        new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
                    }
                }
                if (Height != 0)
                {
                    new_height = (double)m_src_image.Height * Height / 100;

                    if (PreserveAspectRatio)
                    {
                        new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
                    }
                }
            }
            else
            {
                // If you specified an aspect ratio and absolute width or height, then calculate this 
                // now; if you accidentally specified both a width and height, ignore the 
                // PreserveAspectRatio flag
                if (PreserveAspectRatio)
                {
                    if (Width != 0 &amp;&amp; Height == 0)
                    {
                        new_height = (Width / (double)m_src_image.Width) * m_src_image.Height;
                    }
                    else if (Height != 0 &amp;&amp; Width == 0)
                    {
                        new_width = (Height / (double)m_src_image.Height) * m_src_image.Width;
                    }
                }
            }
            recalculate = true;
            if (recalculate)
            {
                // Calculate the new image
                if (m_dst_image != null)
                {
                    m_dst_image.Dispose();
                    m_graphics.Dispose();
                }
                System.Drawing.Image bitmap = new Bitmap((int)new_width, (int)new_height);
                m_graphics = Graphics.FromImage(bitmap);
                m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                m_graphics.DrawImage(m_src_image, 0, 0, bitmap.Width, bitmap.Height);
                m_dst_image = bitmap;
                // Cache the image and its associated settings
                m_cache = this.MemberwiseClone() as ImageResize;
            }

            return m_dst_image;
        }
        #endregion
        #region Deconstructor
        /// <summary>
        /// Frees all held resources, such as Graphics and Image handles
        /// </summary>
        ~ImageResize()
        {
            // Free resources
            if (m_dst_image != null)
            {
                m_dst_image.Dispose();
                m_graphics.Dispose();
            }

            if (m_src_image != null)
                m_src_image.Dispose();
        }
        #endregion
    }
    #endregion
}

   
     


Create Thumbnail

   
 
// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace crudwork.Utilities
{
  /// <summary>
  /// Image Utility
  /// </summary>
  public static class ImageUtil
  {
    #region Enumerators
    #endregion

    #region Fields
    private static int thumbWidth = 96;
    private static int thumbHeight = 96;
    #endregion

    #region Constructors
    #endregion

    #region Event Methods

    #region System Event Methods
    #endregion

    #region Application Event Methods
    #endregion

    #region Custom Event Methods
    #endregion

    #endregion

    #region Public Methods
    /// <summary>
    /// Create thumbnail from an image filename.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static Image GetThumbnail(string filename)
    {
      return GetThumbnail(Image.FromFile(filename), thumbWidth, thumbHeight);
    }

    /// <summary>
    /// Create thumbnail with the given size from an image filename.
    /// </summary>
    /// <param name="filename"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public static Image GetThumbnail(string filename, int width, int height)
    {
      return GetThumbnail(Image.FromFile(filename), width, height);
    }

    /// <summary>
    /// Create thumbnail from an Image type.
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image GetThumbnail(Image image)
    {
      return GetThumbnail(image, thumbWidth, thumbHeight);
    }

    /// <summary>
    /// Create thumbnail with the given size for an Image type.
    /// </summary>
    /// <param name="image"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public static Image GetThumbnail(Image image, int width, int height)
    {
      if (image == null)
        throw new ArgumentNullException("image");

      return image.GetThumbnailImage(width, height, null, new IntPtr());
    }

    /// <summary>
    /// Read the binary content from a file into a byte array.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static byte[] FileToByteArray(string filename)
    {
      using (Stream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
      {
        byte[] imageData = new Byte[fs.Length];
        fs.Read(imageData, 0, imageData.Length);
        fs.Close();
        return imageData;
      }
    }

    /// <summary>
    /// Convert a byte array to Image type.
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static Image ByteArrayToImage(byte[] value)
    {
      if ((value == null) || (value.Length == 0))
        throw new ArgumentNullException("value");

      return Image.FromStream(new MemoryStream(value));
    }

    /// <summary>
    /// Create a thumbnail image from an image filename.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static byte[] CreateThumbnail(string filename)
    {
      using (MemoryStream s = new MemoryStream())
      using (Image image = Image.FromFile(filename).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr()))
      {
        image.Save(s, ImageFormat.Jpeg);
        return s.ToArray();
      }
    }

    /// <summary>
    /// Create a thumbnail image from a byte array, presumely from an image. 
    /// </summary>
    /// <param name="imageData"></param>
    /// <returns></returns>
    public static byte[] CreateThumbnail(byte[] imageData)
    {
      using (MemoryStream s = new MemoryStream())
      using (Image image = Image.FromStream(new MemoryStream(imageData)).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr()))
      {
        image.Save(s, ImageFormat.Jpeg);
        return s.ToArray();
      }
    }

    /// <summary>
    /// Convert an image to a byte array
    /// </summary>
    /// <param name="imageToConvert"></param>
    /// <param name="formatOfImage"></param>
    /// <returns></returns>
    public static byte[] ConvertImageToByteArray(Image imageToConvert, ImageFormat formatOfImage)
    {
      try
      {
        using (MemoryStream ms = new MemoryStream())
        {
          imageToConvert.Save(ms, formatOfImage);
          return ms.ToArray();
        }
      }
      catch (Exception ex)
      {
        
        throw;
      }
    }

    /// <summary>
    /// Return the ImageFormat type based on the file&#039;s extension
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static ImageFormat GetImageFormat(string filename)
    {
      string ext = new FileInfo(filename).Extension.Substring(1);
      switch (ext.ToUpper())
      {
        case "BMP":
          return ImageFormat.Bmp;
        //case "":
        //  return ImageFormat.Emf;
        //case "":
        //  return ImageFormat.Exif;
        case "GIF":
          return ImageFormat.Gif;
        case "ICO":
          return ImageFormat.Icon;
        case "JPEG":
        case "JPG":
        case "JPE":
          return ImageFormat.Jpeg;
        //case "":
        //  return ImageFormat.MemoryBmp;
        case "PNG":
          return ImageFormat.Png;
        case "TIF":
        case "TIFF":
          return ImageFormat.Tiff;
        case "WMF":
          return ImageFormat.Wmf;
        default:
          return null;

      }
    }
    #endregion

    #region Private Methods
    #endregion

    #region Protected Methods
    #endregion

    #region Properties
    #endregion

    #region Others
    #endregion
  }
}

   
     


Image Zoom

   
  
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Label label1= new System.Windows.Forms.Label();
    private System.Windows.Forms.GroupBox groupBox1= new System.Windows.Forms.GroupBox();
    private System.Windows.Forms.RadioButton radioButton1= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.RadioButton radioButton2= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.RadioButton radioButton3= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.RadioButton radioButton4= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.Label label2= new System.Windows.Forms.Label();
    private System.Windows.Forms.CheckBox checkBox1= new System.Windows.Forms.CheckBox();

    Image im = null;
    Image im2 = null;
    public Form1() {
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(8, 16);
        this.label1.Size = new System.Drawing.Size(200, 240);
        this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                          this.checkBox1,
                                          this.radioButton1,
                                          this.radioButton2,
                                          this.radioButton3,
                                          this.radioButton4});
        this.groupBox1.Location = new System.Drawing.Point(232, 48);
        this.groupBox1.Size = new System.Drawing.Size(72, 128);
        this.checkBox1.Location = new System.Drawing.Point(8, 32);
        this.checkBox1.Size = new System.Drawing.Size(56, 24);
        this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
        this.radioButton1.Location = new System.Drawing.Point(8, 64);
        this.radioButton1.Size = new System.Drawing.Size(16, 24);
        this.radioButton2.Location = new System.Drawing.Point(40, 64);
        this.radioButton2.Size = new System.Drawing.Size(16, 24);
        this.radioButton3.Location = new System.Drawing.Point(8, 96);
        this.radioButton3.Size = new System.Drawing.Size(16, 24);
        this.radioButton4.Location = new System.Drawing.Point(40, 96);
        this.radioButton4.Size = new System.Drawing.Size(16, 24);

        this.label2.Location = new System.Drawing.Point(328, 16);
        this.label2.Size = new System.Drawing.Size(200, 240);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(536, 266);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.groupBox1,
                                      this.label1,
                                      this.label2});
        this.groupBox1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.Text = "Zooming";
        this.label1.Text = "";
        this.groupBox1.Text = "Zoom";
        this.checkBox1.Text = "Paint";
        this.radioButton1.Checked = false;
        this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
        this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
        this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
        this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
    }

    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) { ImageZoom(); }
    private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); }
    private void radioButtons_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); }
    protected void ImageZoom() {
        Graphics g1 = Graphics.FromHwnd(this.label1.Handle);
        Graphics g2 = Graphics.FromHwnd(this.label2.Handle);
        Rectangle rec;
        Rectangle recPart;

        if (this.checkBox1.Checked) {
            if (im == null) ReadImage();

            rec = new Rectangle(0, 0, label1.Width, label1.Height);
            g1.DrawImage(im, rec);

            recPart = new Rectangle(im.Width / 4, im.Height / 4, im.Width / 2, im.Height / 2);
            if (this.radioButton1.Checked)
                recPart = new Rectangle(0, 0, im.Width / 2, im.Height / 2);
            if (this.radioButton2.Checked)
                recPart = new Rectangle(im.Width / 2, 0, im.Width / 2, im.Height / 2);
            if (this.radioButton3.Checked)
                recPart = new Rectangle(0, im.Height / 2, im.Width / 2, im.Height / 2);
            if (this.radioButton4.Checked)
                recPart = new Rectangle(im.Width / 2, im.Height / 2, im.Width / 2, im.Height / 2);
            g2.DrawImage(im, rec, recPart, GraphicsUnit.Pixel);
        } else {
            Clear(g1);
            Clear(g2);
        }

        g1.Dispose(); g2.Dispose();
    }
    protected void ReadImage() {
        string path = "3.BMP";
        im = Image.FromFile(path);
        this.radioButton1.Enabled = true;
        this.radioButton2.Enabled = true;
        this.radioButton3.Enabled = true;
        this.radioButton4.Enabled = true;
    }
    protected void Clear(Graphics g) {
        g.Clear(this.BackColor);
        g.Dispose();
        im = null;
        im2 = null;
        this.radioButton1.Checked = false;
        this.radioButton2.Checked = false;
        this.radioButton3.Checked = false;
        this.radioButton4.Checked = false;

        this.radioButton1.Enabled = false;
        this.radioButton2.Enabled = false;
        this.radioButton3.Enabled = false;
        this.radioButton4.Enabled = false;
    }
}

   
     


Image.GetThumbnailImage

   
  
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Net;
using System.IO;

public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
        string p = "s.JPG";
        Image i = Image.FromFile(p);
        Image tn = i.GetThumbnailImage(50, 50, null, IntPtr.Zero);  // <=>(IntPtr)0
        e.Graphics.DrawImage(tn, 100, 0, tn.Width, tn.Height);

        e.Graphics.Dispose();
    }
}