Csharp/C Sharp/2D Graphics/Bitmap — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:41, 26 мая 2010

Bitmap.HorizontalResolution

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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.DrawImage(bmp, 0, 0);
    Console.WriteLine("Screen resolution: " + g.DpiX + "DPI");
    Console.WriteLine("Image resolution: " + bmp.HorizontalResolution + "DPI");
    Console.WriteLine("Image Width: " + bmp.Width);
    Console.WriteLine("Image Height: " + bmp.Height);
    SizeF s = new SizeF(bmp.Width * (g.DpiX / bmp.HorizontalResolution),
              bmp.Height * (g.DpiY / bmp.VerticalResolution));
    Console.WriteLine("Display size of image: " + s);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Bitmap property: Height, Physical Dimension, width, raw format and size

using System;
using System.IO;
using System.Drawing;
   
class MainClass
{
    static public void Main()
    {
        Bitmap img = new Bitmap("winter.jpg");
        
        Console.WriteLine(img.PhysicalDimension.ToString() );
        Console.WriteLine(img.Height.ToString() );
        Console.WriteLine(img.Width.ToString() );
        Console.WriteLine(img.RawFormat.ToString() );
        Console.WriteLine(img.Size.ToString() );
    }
}


Bitmap.SetResolution

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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);
    bmp.SetResolution(600f, 600f);
    g.DrawImage(bmp, 0, 0);
    bmp.SetResolution(1200f, 1200f);
    g.DrawImage(bmp, 180, 0);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Bit operation with PixelFormat.Alpha

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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 & PixelFormat.Alpha) != 0);
        bool b2 = ((bmp2.PixelFormat & 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();
    }
}


Create a Bitmap image in memory and set its CompositingMode

 

using System;
using System.Collections.Generic;
using System.ruponentModel;
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);
    for (int i = 1; i <= 7; ++i)
    {
      Rectangle r = new Rectangle(i * 40 - 15, 0, 15,
                    this.ClientRectangle.Height);
      gForm.FillRectangle(Brushes.Orange, r);
    }
    Bitmap bmp = new Bitmap(260, 260,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    Graphics gBmp = Graphics.FromImage(bmp);
    gBmp.rupositingMode = System.Drawing.Drawing2D.rupositingMode.SourceCopy;
    gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
    bmp.Dispose();
    gBmp.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Create a green color with an alpha component then draw a green rectangle to the bitmap in memory

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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);
        for (int i = 1; i <= 7; ++i) {
            Rectangle r = new Rectangle(i * 40 - 15, 0, 15,
                                        this.ClientRectangle.Height);
            gForm.FillRectangle(Brushes.Orange, r);
        }
        Bitmap bmp = new Bitmap(260, 260, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        Graphics gBmp = Graphics.FromImage(bmp);
        Color green = Color.FromArgb(0x40, 0, 0xff, 0);
        Brush greenBrush = new SolidBrush(green);
        gBmp.FillRectangle(greenBrush, 10, 10, 140, 140);
        gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
        bmp.Dispose();
        gBmp.Dispose();
        greenBrush.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Create a red color with an alpha component then draw a red circle to the bitmap in memory

 

using System;
using System.Collections.Generic;
using System.ruponentModel;
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);
    for (int i = 1; i <= 7; ++i)
    {
      Rectangle r = new Rectangle(i * 40 - 15, 0, 15,this.ClientRectangle.Height);
      gForm.FillRectangle(Brushes.Orange, r);
    }
    Bitmap bmp = new Bitmap(260, 260,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    Graphics gBmp = Graphics.FromImage(bmp);

    Color red = Color.FromArgb(0x60, 0xff, 0, 0);
    Brush redBrush = new SolidBrush(red);
    gBmp.FillEllipse(redBrush, 70, 70, 160, 160);
    gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
    bmp.Dispose();
    gBmp.Dispose();
    redBrush.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Create Graphics object From Image

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class HelloWorldBitmap: Form
{
     const  float fResolution = 300;
     Bitmap bitmap = new Bitmap(1, 1);
   
     public static void Main()
     {
          Application.Run(new HelloWorldBitmap());
     }
     public HelloWorldBitmap()
     {
          ResizeRedraw = true; 
          
          bitmap.SetResolution(fResolution, fResolution);
   
          Graphics grfx = Graphics.FromImage(bitmap);
          Font     font = new Font("Times New Roman", 72);
          Size     size = grfx.MeasureString(Text, font).ToSize();
   
          bitmap = new Bitmap(bitmap, size);
          bitmap.SetResolution(fResolution, fResolution);
               
          grfx = Graphics.FromImage(bitmap);
          grfx.Clear(Color.White);
          grfx.DrawString(Text, font, Brushes.Black, 0, 0);
          grfx.Dispose();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          grfx.DrawImage(bitmap, 0, 0);
     }
}


Create your own BitMap

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Bitmap bmp = new Bitmap(100, 100);
      Graphics gImage = Graphics.FromImage(bmp);
      gImage.FillRectangle(Brushes.Red, 0, 0, bmp.Width, bmp.Height);
      gImage.DrawRectangle(Pens.Black, 10, 10, bmp.Width - 20, bmp.Height - 20);
      Graphics gScreen = e.Graphics;
      gScreen.FillRectangle(Brushes.White, this.ClientRectangle);
      gScreen.DrawImage(bmp, new Rectangle(10, 10, bmp.Width, bmp.Height));
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Double buffer with Bitmap

 
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();
    }
}


Draw an array of images

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
    }
}


Draw on an Bitmap

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Bitmap bmp = new Bitmap(100, 100);
    Graphics gImage = Graphics.FromImage(bmp);
    gImage.FillRectangle(Brushes.Red, 0, 0, bmp.Width, bmp.Height);
    gImage.DrawRectangle(Pens.Black, 10, 10, bmp.Width - 20, bmp.Height - 20);
    Graphics gScreen = e.Graphics;
    gScreen.FillRectangle(Brushes.White, this.ClientRectangle);
    gScreen.DrawImage(bmp, new Rectangle(10, 10, bmp.Width, bmp.Height));
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Draw shapes to the bitmap in memory

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics gForm = e.Graphics;
      gForm.FillRectangle(Brushes.White, this.ClientRectangle);
      // Create a Bitmap image in memory and set its CompositingMode
      Bitmap bmp = new Bitmap(260, 260, PixelFormat.Format32bppArgb);
      Graphics gBmp = Graphics.FromImage(bmp);
      gBmp.rupositingMode = CompositingMode.SourceCopy;
      // draw a red circle to the bitmap in memory
      Color red = Color.FromArgb(0x60, 0xff, 0, 0);
      Brush redBrush = new SolidBrush(red);
      gBmp.FillEllipse(redBrush, 70, 70, 160, 160);
      // draw a green rectangle to the bitmap in memory
      Color green = Color.FromArgb(0x40, 0, 0xff, 0);
      Brush greenBrush = new SolidBrush(green);
      gBmp.FillRectangle(greenBrush, 10, 10, 140, 140);
      
      // draw the bitmap on our window
      gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);
      bmp.Dispose();
      gBmp.Dispose();
      redBrush.Dispose();
      greenBrush.Dispose();
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


new Bitmap(bitmap, size)

 

using System;
using System.Drawing;
using System.Windows.Forms;
class HelloWorldBitmap : Form {
    const float fResolution = 300;
    Bitmap bitmap = new Bitmap(1, 1);
    public static void Main() {
        Application.Run(new HelloWorldBitmap());
    }
    public HelloWorldBitmap() {
        Text = "Hello, World!";
        ResizeRedraw = true;
        bitmap.SetResolution(fResolution, fResolution);
        Graphics grfx = Graphics.FromImage(bitmap);
        Font font = new Font("Times New Roman", 72);
        Size size = grfx.MeasureString(Text, font).ToSize();
        bitmap = new Bitmap(bitmap, size);
        bitmap.SetResolution(fResolution, fResolution);
        grfx = Graphics.FromImage(bitmap);
        grfx.Clear(Color.White);
        grfx.DrawString(Text, font, Brushes.Black, 0, 0);
        grfx.Dispose();
    }
    protected override void OnPaint(PaintEventArgs pea) {
        pea.Graphics.DrawImage(bitmap, 0, 0);
    }
}


PixelFormat.DontCare

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
    }
}


Read Bitmap Size by using BinaryReader

 

using System;
using System.IO;

class Class1 {
    static void Main(string[] args) {
        string[] cma = Environment.GetCommandLineArgs();
        if (cma.GetUpperBound(0) >= 1) {
            FileStream myFStream = new FileStream(cma[1], FileMode.Open, FileAccess.Read);
            BinaryReader binRead = new BinaryReader(myFStream);
            binRead.BaseStream.Position = 0x12;
            
            Console.WriteLine(binRead.ReadInt32());
            Console.WriteLine(binRead.ReadInt32());
            Console.WriteLine(binRead.ReadInt16());
            Console.WriteLine(binRead.ReadInt16());
            binRead.Close();
            myFStream.Close();
        }
    }
}


Resize the Bitmap using the highest quality interpolation mode

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
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);
    int width = bmp.Width;
    int height = bmp.Height;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(
      bmp,
      new Rectangle(130, 10, 120, 120),   // source rectangle
      new Rectangle(0, 0, width, height), // destination rectangle
      GraphicsUnit.Pixel);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Resize the Bitmap using the lowest quality interpolation mode

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
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);
    int width = bmp.Width;
    int height = bmp.Height;
    g.InterpolationMode = InterpolationMode.NearestNeighbor;
    g.DrawImage(
      bmp,
      new Rectangle(10, 10, 120, 120),    // source rectangle
      new Rectangle(0, 0, width, height), // destination rectangle
      GraphicsUnit.Pixel);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Use a color matrix to change the color properties of the image

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing.Printing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
        Bitmap bmp = new Bitmap("alphabet.gif");
        Graphics g = e.Graphics;
        float[][] matrixItems = {
                    new float[] {0.2f, 0, 0, 0, 0},
                    new float[] {0, 0.8f, 0, 0, 0},
                    new float[] {0, 0, 1, 0, 0},
                    new float[] {0, 0, 0, 1, 0}, 
                    new float[] {0, 0, 0, 0, 1}};
        ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
        ImageAttributes imageAtt = new ImageAttributes();
        imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        TextureBrush tb = new TextureBrush(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), imageAtt);
        tb.WrapMode = WrapMode.Tile;
        g.FillRectangle(tb, this.ClientRectangle);
        bmp.Dispose();
        tb.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


write the pixel information to the console window

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
    }
}