Csharp/C Sharp by API/System.Drawing.Imaging/PixelFormat

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

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


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


PixelFormat.Format24bppRgb

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


PixelFormat.Format32bppArgb

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