Csharp/C Sharp by API/System.Drawing/Bitmap
Содержание
Bitmap.CompositingMode
<source lang="csharp"> 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()); }
}
</source>
Bitmap.Height
<source lang="csharp">
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 g = e.Graphics; Bitmap bmp = new Bitmap("winter.jpg"); Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height); g.DrawImage(bmp, r, r, GraphicsUnit.Pixel); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Bitmap.HorizontalResolution
<source lang="csharp">
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 g = e.Graphics; Bitmap bmp = new Bitmap("winter.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); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Bitmap.MakeTransparent( );
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace BitmapOther_c {
/// <summary> /// Summary description for BitmapOther. /// </summary> public class BitmapOther : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public BitmapOther() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.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() { // // BitmapOther // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Name = "BitmapOther"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "BitmapOther"; this.Load += new System.EventHandler(this.BitmapOther_Load); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new BitmapOther()); } private void BitmapOther_Load(object sender, System.EventArgs e) { } protected override void OnPaint ( PaintEventArgs e ) { Bitmap BMP = new Bitmap("Colorbars.JPG"); Point Pt = new Point(20,20); // BMP.SetPixel(15,20,Color.Black); BMP.MakeTransparent( BMP.GetPixel(15,25) ); e.Graphics.DrawImage(BMP, Pt); e.Graphics.DrawLine(new Pen(Brushes.GreenYellow,30),60,60,200,60);
} } } </source>
Bitmap.PhysicalDimension
<source lang="csharp">
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() ); }
}
</source>
Bitmap.PixelFormat
<source lang="csharp">
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(); }
}
</source>
Bitmap.RawFormat
<source lang="csharp">
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() ); }
}
</source>
Bitmap.SetResolution(600f, 600f);
<source lang="csharp">
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 g = e.Graphics; Bitmap bmp = new Bitmap("winter.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); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
Bitmap.Size
<source lang="csharp"> 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() ); }
}
</source>
Bitmap.Width
<source lang="csharp">
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 g = e.Graphics; Bitmap bmp = new Bitmap("winter.jpg"); Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height); g.DrawImage(bmp, this.ClientRectangle); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
new Bitmap(String fileName)
<source lang="csharp"> 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"); Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height); g.DrawImage(bmp, r, r, GraphicsUnit.Pixel); } public static void Main() { Application.Run(new Form1()); }
}
</source>