Blend Alpha

image_pdfimage_print



   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Blend_c
{ 
    public class Blend : System.Windows.Forms.Form
    {
    private System.Windows.Forms.HScrollBar AlphaScroll;
    private System.Windows.Forms.HScrollBar GammaScroll;
    /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

    private int AlphaFactor = 255;
    private float GammaFactor = 1.0f;
    private Rectangle R = new Rectangle(40, 20, 100, 100 );
    private Image I = Image.FromFile("Colorbars.jpg");
    private int ImWidth;
    private int ImHeight;
    private ImageAttributes Ia = new ImageAttributes();



        public Blend()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

      AlphaScroll.Minimum = 20;
      AlphaScroll.Maximum = 245;
      AlphaScroll.SmallChange = 5;
      AlphaScroll.LargeChange = 5;
      AlphaScroll.Left = R.Left;
      AlphaScroll.Width = R.Width;
      AlphaScroll.Top = R.Bottom;

      GammaScroll.Minimum=1;
      GammaScroll.Maximum = 50;
      GammaScroll.SmallChange=1;
      GammaScroll.LargeChange=5;
      GammaScroll.Left = R.Left;
      GammaScroll.Top = R.Top - GammaScroll.Height;
      GammaScroll.Width = R.Width;

      ImWidth = I.Width;
      ImHeight = I.Height;

      AlphaScroll.Value = (AlphaScroll.Maximum-AlphaScroll.Minimum )/2;
      GammaScroll.Value = (GammaScroll.Maximum-GammaScroll.Minimum )/2;
      AlphaFactor = AlphaScroll.Value;
      GammaFactor = (float)GammaScroll.Value / 10;

    }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
        if ( I != null )
          I.Dispose();
        if ( Ia != null )
          Ia.Dispose();
      }

            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.AlphaScroll = new System.Windows.Forms.HScrollBar();
      this.GammaScroll = new System.Windows.Forms.HScrollBar();
      this.SuspendLayout();
      // 
      // AlphaScroll
      // 
      this.AlphaScroll.Location = new System.Drawing.Point(32, 128);
      this.AlphaScroll.Maximum = 255;
      this.AlphaScroll.Name = "AlphaScroll";
      this.AlphaScroll.Size = new System.Drawing.Size(160, 16);
      this.AlphaScroll.TabIndex = 1;
      this.AlphaScroll.Scroll += new System.Windows.Forms.ScrollEventHandler(this.AlphaScroll_Scroll);
      // 
      // GammaScroll
      // 
      this.GammaScroll.Location = new System.Drawing.Point(32, 8);
      this.GammaScroll.Name = "GammaScroll";
      this.GammaScroll.Size = new System.Drawing.Size(160, 16);
      this.GammaScroll.TabIndex = 2;
      this.GammaScroll.Scroll += new System.Windows.Forms.ScrollEventHandler(this.GammaScroll_Scroll);
      // 
      // Blend
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.GammaScroll,
                                                                  this.AlphaScroll});
      this.Name = "Blend";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "Blend";
      this.Load += new System.EventHandler(this.Blend_Load);
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Blend());
        }

    private void Blend_Load(object sender, System.EventArgs e)
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      AlphaBlend(e.Graphics);
      base.OnPaint(e);
    }

    private void AlphaBlend( Graphics G )
    {
      //AlphaFactor is variable depeneding upon scroll bars
      Pen P = new Pen( Color.FromArgb (AlphaFactor, 200, 0, 100 ), 20);
      Bitmap bmp = new Bitmap( 120, 120 );
      Graphics G2 = Graphics.FromImage(bmp);
      Brush B = new SolidBrush(Color.FromArgb( AlphaFactor, 50, 200, 50 ));

      try
      {
        // Set the brightness while rendering image
        Ia.SetGamma( GammaFactor );
        G.DrawImage(I, R, 0, 0, ImWidth, ImHeight, GraphicsUnit.Pixel, Ia);
        //Draw transparent line on top of image
        G.DrawLine(P, 10, 100, 200, 100 );

        // Draw inside the image contained in memory
        G2.FillEllipse( B, 0, 0, 75, 75 );
        G.DrawImage( I, new Rectangle(140, 140, 120, 120 ) );
        G.CompositingQuality = CompositingQuality.GammaCorrected;
        G.CompositingMode = CompositingMode.SourceOver;
        G.DrawImage( bmp, new Rectangle( 150, 150, 150, 150 ) );
      }
      finally
      {
        if (bmp != null )
          bmp.Dispose();
        if ( G2 != null )
          G2.Dispose();
        if ( B != null )
          B.Dispose();
        if ( P != null )
          P.Dispose();
      }
    }

    private void AlphaScroll_Scroll(object sender, 
                                    System.Windows.Forms.ScrollEventArgs e)
    {
      AlphaFactor = AlphaScroll.Value;
      this.Refresh();
    }

    private void GammaScroll_Scroll(object sender, 
                                    System.Windows.Forms.ScrollEventArgs e)
    {
      GammaFactor = (float)GammaScroll.Value / 10;
      this.Refresh();
    }

    }
}



           
          


Blend-c.zip( 5 k)

Scale Bitmap By Percent

image_pdfimage_print
   
 

//Octavalent Extension Methods
//http://sdfasdf.codeplex.com/
//Library of extension methods for .Net create by Octavalent (www.octavalent.nl)


#region

using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

#endregion

namespace System.Drawing.OctavalentExtensions
{
    public static class BitmapExtensions
    {
        public static Bitmap ScaleByPercent(this Bitmap imgPhoto, int Percent)
        {
            float nPercent = ((float) Percent/100);

            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            var destWidth = (int) (sourceWidth*nPercent);
            var destHeight = (int) (sourceHeight*nPercent);

            var bmPhoto = new Bitmap(destWidth, destHeight,
                                     PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                                  imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                              new Rectangle(0, 0, destWidth, destHeight),
                              new Rectangle(0, 0, sourceWidth, sourceHeight),
                              GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
    }
}

   
     


PixelFormat.DontCare

image_pdfimage_print
   
  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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);

    Rectangle r = new Rectangle(120, 120, 400, 400);
    Bitmap bmp2 = bmp.Clone(r, System.Drawing.Imaging.PixelFormat.DontCare);
    g.DrawImage(bmp, new Rectangle(0, 0, 200, 200));
    g.DrawImage(bmp2, new Rectangle(210, 0, 200, 200));
    bmp2.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

   
     


Bit operation with PixelFormat.Alpha

image_pdfimage_print
   
  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

using System.Text;
using System.Windows.Forms;

public class MainClass {

    public static void Main() {
        // Create two new bitmap images
        Bitmap bmp1 = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
        Bitmap bmp2 = new Bitmap(100, 100, PixelFormat.Format24bppRgb);

        // Test for alpha 
        bool b1 = ((bmp1.PixelFormat &amp; PixelFormat.Alpha) != 0);
        bool b2 = ((bmp2.PixelFormat &amp; PixelFormat.Alpha) != 0);

        // Output results to console window
        Console.WriteLine("bmp1 has alpha?: " + b1);
        Console.WriteLine("bmp2 has alpha?: " + b2);

        // Clean up
        bmp1.Dispose();
        bmp2.Dispose();
    }
}

   
     


Draw an array of images

image_pdfimage_print


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

public class MainForm : Form {
    private Bitmap[] myImages = new Bitmap[3];
    public MainForm() {
        myImages[0] = new Bitmap("imageA.bmp");
        myImages[1] = new Bitmap("imageB.bmp");
        myImages[2] = new Bitmap("imageC.bmp");
    }

    protected void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        int yOffset = 10;
        foreach (Bitmap b in myImages) {
            g.DrawImage(b, 10, yOffset, 90, 90);
            yOffset += 100;
        }
    }
    public static void Main() {
        Application.Run(new MainForm());
    }
}

   
     


Double buffer with Bitmap

image_pdfimage_print
   
  
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form {
    Pen p;
    SolidBrush b, bT = new SolidBrush(Color.Black);
    string path = "5.bmp";
    Image im;
    Font f;

    public Form1() {
        Color cP = Color.Gray;
        Color cB = Color.LightGray;

        p = new Pen(cP, 6);
        b = new SolidBrush(cB);
        im = Image.FromFile(path);
        f = new Font(new FontFamily("Times New Roman"), 10);
    }
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs pea) {
        Sketch();
        //SketchDBuf();
    }
    private void Sketch() {
        Graphics g = Graphics.FromHwnd(this.Handle);

        g.FillRectangle(b, 4, 4, 260, 220);
        g.DrawRectangle(p, 4, 4, 260, 220);
        g.DrawImage(im, 33, 35, 200, 145);
        g.DrawString("AAAAAA", f, bT, 180, 190);

        g.Dispose();
    }
    private void SketchDBuf() {
        int hh = 3, w = 260, h = 220;

        Graphics g;
        Bitmap bm = new Bitmap(w + 2 * hh, h + 2 * hh);
        g = Graphics.FromImage(bm);

        g.FillRectangle(b, hh, hh, w, h);
        g.DrawRectangle(new Pen(Color.Gray, 2 * hh), hh, hh, w, h);
        g.DrawImage(im, hh + 30, hh + 32, 200, 145);
        g.DrawString("Text", f, bT, 180, 190);
        g = Graphics.FromHwnd(this.Handle);
        g.DrawImage(bm, 1, 1);

        g.Dispose();
    }
}

   
     


write the pixel information to the console window

image_pdfimage_print

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

public class Form1 : Form {

protected override void OnPaint(PaintEventArgs e) {
Graphics gForm = e.Graphics;
gForm.FillRectangle(Brushes.White, this.ClientRectangle);
Bitmap bmp = new Bitmap(6, 6);
Graphics gBmp = Graphics.FromImage(bmp);

gBmp.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
gBmp.DrawLine(Pens.Red, 0, 0, 5, 5);
gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);

for (int y = 0; y < bmp.Height; ++y) { for (int x = 0; x < bmp.Width; ++x) { Color c = bmp.GetPixel(x, y); Console.Write("{0,2:x}{1,2:x}{2,2:x}{3,2:x} ",c.A, c.R, c.G, c.G); } Console.WriteLine(); } bmp.Dispose(); } public static void Main() { Application.Run(new Form1()); } } [/csharp]