using System; using System.Collections.Generic; using System.ComponentModel; 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()); } }
2D Graphics
Bitmap.HorizontalResolution
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.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()); } }
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(); } } }
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); } }
Draw shapes to the bitmap in memory
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; 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.CompositingMode = 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(); } }
Create your own BitMap
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; 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(); } }
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() ); } }