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); 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()); } }
2D Graphics
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); } }
Resize the Bitmap using the highest quality interpolation mode
using System; using System.Collections.Generic; using System.ComponentModel; 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.ComponentModel; 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()); } }
Draw on an Bitmap
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) { 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 Arrow
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; 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; g.SmoothingMode = SmoothingMode.AntiAlias; g.FillRectangle(Brushes.White, this.ClientRectangle); Pen p = new Pen(Color.Black, 10); p.StartCap = LineCap.Round; p.EndCap = LineCap.ArrowAnchor; g.DrawLine(p, 30, 30, 80, 30); p.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } }
Animate Image with ImageAnimator
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 { private Bitmap bmp; public Form1() { } private void Form1_Load(object sender, EventArgs e) { bmp = new Bitmap("arrow.gif"); ImageAnimator.Animate(bmp, new EventHandler(this.OnFrameChanged)); } private void OnFrameChanged(object o, EventArgs e) { this.Invalidate(); } private void Form1_Paint(object sender, PaintEventArgs e) { ImageAnimator.UpdateFrames(); e.Graphics.DrawImage(this.bmp, new Point(0, 0)); } }