Csharp/C Sharp/2D Graphics/Image — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
- 1 Center an Image (VerticalResolution, HorizontalResolution)
- 2 Center Pixel-Size Image by using Image.Width and Image.Height
- 3 Clone Image
- 4 Draw image based on its size
- 5 Draw on Pixel-Size Image
- 6 Draw Partial Image
- 7 Draw text on an Image
- 8 Fill Ellipse with image based Texture Brush
- 9 Get Image Resolution and Image size and Display size
- 10 Image At Points (Draw part of the image)
- 11 Image Flipping and Rotating
- 12 Image.FromStream: load image from stream
- 13 Image.GetThumbnailImage
- 14 Image Open
- 15 Image Reflection
- 16 Image Save
- 17 Image Scale Isotropic
- 18 Image Scale To Rectangle
- 19 Image Zoom
- 20 Load image and display
- 21 Load Image from an image file with Exception handler
- 22 Load image from a URL(Web) and draw it
- 23 Partial Image Rotate
- 24 Partial Image Stretch
- 25 Scribble with Bitmap
- 26 Set image resolution and paint it
- 27 Shear Image
- 28 Shrink Image
- 29 Thumbnail Image
Center an Image (VerticalResolution, HorizontalResolution)
using System;
using System.Drawing;
using System.Windows.Forms;
class CenterImage: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new CenterImage());
}
public CenterImage()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.PageUnit = GraphicsUnit.Pixel;
grfx.PageScale = 1;
RectangleF rectf = grfx.VisibleClipBounds;
float cxImage = grfx.DpiX * image.Width /
image.HorizontalResolution;
float cyImage = grfx.DpiY * image.Height /
image.VerticalResolution;
grfx.DrawImage(image, (rectf.Width - cxImage) / 2,
(rectf.Height - cyImage) / 2);
}
}
Center Pixel-Size Image by using Image.Width and Image.Height
using System;
using System.Drawing;
using System.Windows.Forms;
class CenterPixelSizeImage: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new CenterPixelSizeImage());
}
public CenterPixelSizeImage()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.DrawImage(image, (cx - image.Width) / 2,
(cy - image.Height) / 2,
image.Width, image.Height);
}
}
Clone Image
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);
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();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw image based on its size
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();
}
}
Draw on Pixel-Size Image
using System;
using System.Drawing;
using System.Windows.Forms;
class DrawOnPixelSizeImage: Form
{
Image image = Image.FromFile("Color.jpg");
string str = "www.nfex.ru";
public static void Main()
{
Application.Run(new DrawOnPixelSizeImage());
}
public DrawOnPixelSizeImage()
{
ResizeRedraw = true;
Graphics grfxImage = Graphics.FromImage(image);
Graphics grfxScreen = CreateGraphics();
Font font = new Font(Font.FontFamily, grfxScreen.DpiY / grfxImage.DpiY * Font.SizeInPoints);
SizeF sizef = grfxImage.MeasureString(str, font);
grfxImage.DrawString(str, font, Brushes.White, image.Width - sizef.Width, 0);
grfxImage.Dispose();
grfxScreen.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.DrawImage(image, 0, 0, image.Width, image.Height);
grfx.DrawString(str, Font, new SolidBrush(clr), image.Width, 0);
}
}
Draw Partial Image
using System;
using System.Drawing;
using System.Windows.Forms;
class PartialImage: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new PartialImage());
}
public PartialImage()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Rectangle rect = new Rectangle(95, 5, 50, 55);
grfx.DrawImage(image, 0, 0, rect, GraphicsUnit.Pixel);
}
}
Draw text on an Image
using System;
using System.Drawing;
using System.Windows.Forms;
class DrawOnImage: Form
{
Image image = Image.FromFile("Color.jpg");
string str = "www.nfex.ru";
public static void Main()
{
Application.Run(new DrawOnImage());
}
public DrawOnImage()
{
ResizeRedraw = true;
Graphics grfxImage = Graphics.FromImage(image);
grfxImage.PageUnit = GraphicsUnit.Inch;
grfxImage.PageScale = 1;
SizeF sizef = grfxImage.MeasureString(str, Font);
grfxImage.DrawString(str, Font, Brushes.White, grfxImage.VisibleClipBounds.Width - sizef.Width, 0);
grfxImage.Dispose();
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.PageUnit = GraphicsUnit.Pixel;
grfx.DrawImage(image, 0, 0);
grfx.DrawString(str, Font, new SolidBrush(clr),
grfx.DpiX * image.Width / image.HorizontalResolution, 0);
}
}
Fill Ellipse with image based Texture Brush
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.Container components = null;
private Image theImage;
private Image smallImage;
public Form1() {
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
theImage = new Bitmap("Winter.jpg");
smallImage = new Bitmap(theImage,new Size(theImage.Width / 2, theImage.Height / 2));
}
protected override void OnPaint(PaintEventArgs e){
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
Brush tBrush = new TextureBrush(smallImage, new Rectangle(0, 0,smallImage.Width, smallImage.Height));
g.FillEllipse(tBrush, ClientRectangle);
tBrush.Dispose();
}
private void InitializeComponent() {
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main() {
Application.Run(new Form1());
}
}
Get Image Resolution and Image size and Display size
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();
}
}
Image At Points (Draw part of the image)
using System;
using System.Drawing;
using System.Windows.Forms;
class ImageAtPoints: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new ImageAtPoints());
}
public ImageAtPoints()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.DrawImage(image, new Point[] { new Point(cx / 2, 0),
new Point(cx, cy / 2),
new Point(0, cy / 2)});
}
}
Image Flipping and Rotating
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.RadioButton radioButton1;
Image im = null;
Image im2 = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
public Form1() {
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.radioButton3,
this.radioButton2,
this.radioButton1,
this.checkBox1});
this.groupBox1.Location = new System.Drawing.Point(312, 64);
this.groupBox1.Size = new System.Drawing.Size(248, 80);
this.radioButton1.Location = new System.Drawing.Point(120, 16);
this.radioButton1.Size = new System.Drawing.Size(112, 24);
this.checkBox1.Location = new System.Drawing.Point(16, 16);
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Size = new System.Drawing.Size(304, 200);
this.radioButton2.Location = new System.Drawing.Point(16, 48);
this.radioButton3.Location = new System.Drawing.Point(120, 48);
this.radioButton3.Size = new System.Drawing.Size(120, 24);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 214);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.groupBox1});
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
this.radioButton1.Checked = false;
this.label1.Text = "";
this.groupBox1.Text = "RotateFlipType";
this.checkBox1.Text = "Paint";
this.radioButton1.Text = "Rotate180FlipY";
this.radioButton2.Text = "Rotate180FlipX";
this.radioButton3.Text = "Rotate180FlipNone";
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
protected override void OnPaint(PaintEventArgs e) { RotateFlip(); }
private void label1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { RotateFlip(); }
private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { RotateFlip(); }
private void radioButtons_CheckedChanged(object sender, System.EventArgs e) { RotateFlip(); }
protected void RotateFlip() {
Graphics g = Graphics.FromHwnd(this.label1.Handle);
Brush b = new SolidBrush(this.label1.BackColor);
if (this.checkBox1.Checked) {
if (im == null) ReadImage();
Graphics g2 = Graphics.FromImage(im);
FontFamily ff = new FontFamily("Times New Roman");
Font f = new Font(ff, 25, FontStyle.Bold);
g2.DrawString("HIMALAYA", f, new SolidBrush(Color.Yellow), 170, 210);
g2.Dispose();
im2 = (Image)im.Clone();
int w2 = label1.Width / 2, h2 = label1.Height / 2;
g.DrawImage(im, 0, 0, w2, h2);
if (this.radioButton1.Checked){
im2.RotateFlip(RotateFlipType.Rotate180FlipY);
g.DrawImage(im2, w2, 0, w2, h2);
} else
g.FillRectangle(b, w2, 0, w2, h2);
if (this.radioButton2.Checked) {
im2.RotateFlip(RotateFlipType.Rotate180FlipX);
g.DrawImage(im2, 0, h2, w2, h2);
} else
g.FillRectangle(b, 0, h2, w2, h2);
if (this.radioButton3.Checked) {
im2.RotateFlip(RotateFlipType.Rotate180FlipNone);
g.DrawImage(im2, w2, h2, w2, h2);
} else
g.FillRectangle(b, w2, h2, w2, h2);
im2.Dispose();
} else Clear(g);
b.Dispose(); g.Dispose();
}
protected void ReadImage() {
string path = "a.bmp";
im = Image.FromFile(path);
this.radioButton1.Enabled = true;
this.radioButton2.Enabled = true;
this.radioButton3.Enabled = true;
}
protected void Clear(Graphics g) {
g.Clear(this.BackColor);
g.Dispose();
im = null;
im2 = null;
this.radioButton1.Checked = false;
this.radioButton2.Checked = false;
this.radioButton3.Checked = false;
this.radioButton1.Enabled = false;
this.radioButton2.Enabled = false;
this.radioButton3.Enabled = false;
}
}
Image.FromStream: load image from stream
using System;
using System.Net;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
public class MainClass {
public static void Main() {
WebRequest requestPic = WebRequest.Create("http://www.your.ru/1.jpg");
WebRequest requestHtml = WebRequest.Create("http://www.your.ru");
WebResponse responsePic = requestPic.GetResponse();
WebResponse responseHtml = requestHtml.GetResponse();
Image img = Image.FromStream(responsePic.GetResponseStream());
using (StreamReader r = new StreamReader(responseHtml.GetResponseStream())) {
Console.WriteLine(r.ReadToEnd());
}
}
}
Image.GetThumbnailImage
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
public class Form1 : System.Windows.Forms.Form {
[STAThread]
static void Main() {
Application.Run(new Form1());
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
string p = "s.JPG";
Image i = Image.FromFile(p);
Image tn = i.GetThumbnailImage(50, 50, null, IntPtr.Zero); // <=>(IntPtr)0
e.Graphics.DrawImage(tn, 100, 0, tn.Width, tn.Height);
e.Graphics.Dispose();
}
}
Image Open
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
class ImageOpen: Form
{
protected string strProgName;
protected string strFileName;
protected Image image;
public static void Main()
{
Application.Run(new ImageOpen());
}
public ImageOpen()
{
ResizeRedraw = true;
Menu = new MainMenu();
Menu.MenuItems.Add("&File");
Menu.MenuItems[0].MenuItems.Add(new MenuItem("&Open...",
new EventHandler(MenuFileOpenOnClick),
Shortcut.CtrlO));
}
void MenuFileOpenOnClick(object obj, EventArgs ea)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" +
"*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" +
"Windows Bitmap (*.bmp)|*.bmp|" +
"Windows Icon (*.ico)|*.ico|" +
"Graphics Interchange Format (*.gif)|*.gif|" +
"JPEG File Interchange Format (*.jpg)|" +
"*.jpg;*.jpeg;*.jfif|" +
"Portable Network Graphics (*.png)|*.png|" +
"Tag Image File Format (*.tif)|*.tif;*.tiff|" +
"Windows Metafile (*.wmf)|*.wmf|" +
"Enhanced Metafile (*.emf)|*.emf|" +
"All Files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
image = Image.FromFile(dlg.FileName);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
return;
}
strFileName = dlg.FileName;
Text = Path.GetFileName(strFileName);
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
if (image != null)
grfx.DrawImage(image, 0, 0);
}
}
Image Reflection
using System;
using System.Drawing;
using System.Windows.Forms;
class ImageReflection: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new ImageReflection());
}
public ImageReflection()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
int cxImage = image.Width;
int cyImage = image.Height;
grfx.DrawImage(image, cx / 2, cy / 2, cxImage, cyImage);
grfx.DrawImage(image, cx / 2, cy / 2, -cxImage, cyImage);
grfx.DrawImage(image, cx / 2, cy / 2, cxImage, -cyImage);
grfx.DrawImage(image, cx / 2, cy / 2, -cxImage, -cyImage);
}
}
Image Save
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
class ImageOpen: Form
{
protected string strProgName;
protected string strFileName;
protected Image image;
MenuItem miSaveAs;
public static void Main()
{
Application.Run(new ImageOpen());
}
public ImageOpen()
{
ResizeRedraw = true;
Menu = new MainMenu();
Menu.MenuItems.Add("&File");
Menu.MenuItems[0].MenuItems.Add(new MenuItem("&Open...",
new EventHandler(MenuFileOpenOnClick),
Shortcut.CtrlO));
Menu.MenuItems[0].Popup += new EventHandler(MenuFileOnPopup);
miSaveAs = new MenuItem("Save &As...");
miSaveAs.Click += new EventHandler(MenuFileSaveAsOnClick);
Menu.MenuItems[0].MenuItems.Add(miSaveAs);
}
void MenuFileOnPopup(object obj, EventArgs ea)
{
miSaveAs.Enabled = (image != null);
}
void MenuFileSaveAsOnClick(object obj, EventArgs ea)
{
SaveFileDialog savedlg = new SaveFileDialog();
savedlg.InitialDirectory = Path.GetDirectoryName(strFileName);
savedlg.FileName = Path.GetFileNameWithoutExtension(strFileName);
savedlg.AddExtension = true;
savedlg.Filter = "Windows Bitmap (*.bmp)|*.bmp|" +
"Graphics Interchange Format (*.gif)|*.gif|" +
"JPEG File Interchange Format (*.jpg)|" +
"*.jpg;*.jpeg;*.jfif|" +
"Portable Network Graphics (*.png)|*.png|" +
"Tagged Imaged File Format (*.tif)|*.tif;*.tiff";
if (savedlg.ShowDialog() == DialogResult.OK)
{
try
{
image.Save(savedlg.FileName);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, Text);
return;
}
strFileName = savedlg.FileName;
Text = strProgName + " - " + Path.GetFileName(strFileName);
}
}
void MenuFileOpenOnClick(object obj, EventArgs ea)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" +
"*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" +
"Windows Bitmap (*.bmp)|*.bmp|" +
"Windows Icon (*.ico)|*.ico|" +
"Graphics Interchange Format (*.gif)|*.gif|" +
"JPEG File Interchange Format (*.jpg)|" +
"*.jpg;*.jpeg;*.jfif|" +
"Portable Network Graphics (*.png)|*.png|" +
"Tag Image File Format (*.tif)|*.tif;*.tiff|" +
"Windows Metafile (*.wmf)|*.wmf|" +
"Enhanced Metafile (*.emf)|*.emf|" +
"All Files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
image = Image.FromFile(dlg.FileName);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
return;
}
strFileName = dlg.FileName;
Text = Path.GetFileName(strFileName);
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
if (image != null)
grfx.DrawImage(image, 0, 0);
}
}
Image Scale Isotropic
using System;
using System.Drawing;
using System.Windows.Forms;
class ImageScaleIsotropic: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new ImageScaleIsotropic());
}
public ImageScaleIsotropic()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Rectangle rect = new Rectangle(0, 0, cx, cy);
SizeF sizef = new SizeF(image.Width / image.HorizontalResolution,
image.Height / image.VerticalResolution);
float fScale = Math.Min(rect.Width / sizef.Width,
rect.Height / sizef.Height);
sizef.Width *= fScale;
sizef.Height *= fScale;
grfx.DrawImage(image, rect.X + (rect.Width - sizef.Width ) / 2,
rect.Y + (rect.Height - sizef.Height) / 2,
sizef.Width, sizef.Height);
}
}
Image Scale To Rectangle
using System;
using System.Drawing;
using System.Windows.Forms;
class ImageScaleToRectangle: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new ImageScaleToRectangle());
}
public ImageScaleToRectangle()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.DrawImage(image, 0, 0, cx, cy);
}
}
Image Zoom
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Label label1= new System.Windows.Forms.Label();
private System.Windows.Forms.GroupBox groupBox1= new System.Windows.Forms.GroupBox();
private System.Windows.Forms.RadioButton radioButton1= new System.Windows.Forms.RadioButton();
private System.Windows.Forms.RadioButton radioButton2= new System.Windows.Forms.RadioButton();
private System.Windows.Forms.RadioButton radioButton3= new System.Windows.Forms.RadioButton();
private System.Windows.Forms.RadioButton radioButton4= new System.Windows.Forms.RadioButton();
private System.Windows.Forms.Label label2= new System.Windows.Forms.Label();
private System.Windows.Forms.CheckBox checkBox1= new System.Windows.Forms.CheckBox();
Image im = null;
Image im2 = null;
public Form1() {
this.groupBox1.SuspendLayout();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Size = new System.Drawing.Size(200, 240);
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.checkBox1,
this.radioButton1,
this.radioButton2,
this.radioButton3,
this.radioButton4});
this.groupBox1.Location = new System.Drawing.Point(232, 48);
this.groupBox1.Size = new System.Drawing.Size(72, 128);
this.checkBox1.Location = new System.Drawing.Point(8, 32);
this.checkBox1.Size = new System.Drawing.Size(56, 24);
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
this.radioButton1.Location = new System.Drawing.Point(8, 64);
this.radioButton1.Size = new System.Drawing.Size(16, 24);
this.radioButton2.Location = new System.Drawing.Point(40, 64);
this.radioButton2.Size = new System.Drawing.Size(16, 24);
this.radioButton3.Location = new System.Drawing.Point(8, 96);
this.radioButton3.Size = new System.Drawing.Size(16, 24);
this.radioButton4.Location = new System.Drawing.Point(40, 96);
this.radioButton4.Size = new System.Drawing.Size(16, 24);
this.label2.Location = new System.Drawing.Point(328, 16);
this.label2.Size = new System.Drawing.Size(200, 240);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(536, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1,
this.label1,
this.label2});
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
this.Text = "Zooming";
this.label1.Text = "";
this.groupBox1.Text = "Zoom";
this.checkBox1.Text = "Paint";
this.radioButton1.Checked = false;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
protected override void OnPaint(PaintEventArgs e) { ImageZoom(); }
private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); }
private void radioButtons_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); }
protected void ImageZoom() {
Graphics g1 = Graphics.FromHwnd(this.label1.Handle);
Graphics g2 = Graphics.FromHwnd(this.label2.Handle);
Rectangle rec;
Rectangle recPart;
if (this.checkBox1.Checked) {
if (im == null) ReadImage();
rec = new Rectangle(0, 0, label1.Width, label1.Height);
g1.DrawImage(im, rec);
recPart = new Rectangle(im.Width / 4, im.Height / 4, im.Width / 2, im.Height / 2);
if (this.radioButton1.Checked)
recPart = new Rectangle(0, 0, im.Width / 2, im.Height / 2);
if (this.radioButton2.Checked)
recPart = new Rectangle(im.Width / 2, 0, im.Width / 2, im.Height / 2);
if (this.radioButton3.Checked)
recPart = new Rectangle(0, im.Height / 2, im.Width / 2, im.Height / 2);
if (this.radioButton4.Checked)
recPart = new Rectangle(im.Width / 2, im.Height / 2, im.Width / 2, im.Height / 2);
g2.DrawImage(im, rec, recPart, GraphicsUnit.Pixel);
} else {
Clear(g1);
Clear(g2);
}
g1.Dispose(); g2.Dispose();
}
protected void ReadImage() {
string path = "3.BMP";
im = Image.FromFile(path);
this.radioButton1.Enabled = true;
this.radioButton2.Enabled = true;
this.radioButton3.Enabled = true;
this.radioButton4.Enabled = true;
}
protected void Clear(Graphics g) {
g.Clear(this.BackColor);
g.Dispose();
im = null;
im2 = null;
this.radioButton1.Checked = false;
this.radioButton2.Checked = false;
this.radioButton3.Checked = false;
this.radioButton4.Checked = false;
this.radioButton1.Enabled = false;
this.radioButton2.Enabled = false;
this.radioButton3.Enabled = false;
this.radioButton4.Enabled = false;
}
}
Load image and display
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
private System.ruponentModel.Container components = null;
private Image theImage;
public Form1() {
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
theImage = new Bitmap("Winter.jpg");
}
protected override void OnPaint(PaintEventArgs e){
Graphics g = e.Graphics;
g.DrawImage(theImage, ClientRectangle);
}
private void InitializeComponent() {
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main() {
Application.Run(new Form1());
}
}
Load Image from an image file with Exception handler
using System;
using System.Drawing;
using System.Windows.Forms;
class BetterImageFromFile:Form
{
Image image;
public static void Main()
{
Application.Run(new BetterImageFromFile());
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
public BetterImageFromFile()
{
string strFileName = "Color.jpg";
ResizeRedraw = true;
try
{
image = Image.FromFile(strFileName);
}catch {
MessageBox.Show("Cannot find file " + strFileName + "!",
Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
if (image == null)
return;
grfx.DrawImage(image, 0, 0);
}
}
Load image from a URL(Web) and draw it
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
class ImageFromWeb: Form
{
Image image;
public static void Main()
{
Application.Run(new ImageFromWeb());
}
public ImageFromWeb()
{
ResizeRedraw = true;
WebRequest webreq = WebRequest.Create("http://www.yoursite.ru/1.jpg");
WebResponse webres = webreq.GetResponse();
Stream stream = webres.GetResponseStream();
image = Image.FromStream(stream);
stream.Close();
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
grfx.DrawImage(image, 0, 0);
}
}
Partial Image Rotate
using System;
using System.Drawing;
using System.Windows.Forms;
class PartialImageRotate: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new PartialImageRotate());
}
public PartialImageRotate()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Point[] aptDst = { new Point(0, cy / 2),
new Point(cx / 2, 0),
new Point(cx / 2, cy) };
Rectangle rectSrc = new Rectangle(95, 5, 50, 55);
grfx.DrawImage(image, aptDst, rectSrc, GraphicsUnit.Pixel);
}
}
Partial Image Stretch
using System;
using System.Drawing;
using System.Windows.Forms;
class PartialImageStretch: Form
{
Image image = Image.FromFile("Color.jpg");
public static void Main()
{
Application.Run(new PartialImageStretch());
}
public PartialImageStretch()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Rectangle rectSrc = new Rectangle(95, 5, 50, 55);
Rectangle rectDst = new Rectangle( 0, 0, cx, cy);
grfx.DrawImage(image, rectDst, rectSrc, GraphicsUnit.Pixel);
}
}
Scribble with Bitmap
using System;
using System.Drawing;
using System.Windows.Forms;
class ScribbleWithBitmap: Form
{
bool bTracking;
Point ptLast;
Bitmap bitmap;
Graphics grfxBm;
public static void Main()
{
Application.Run(new ScribbleWithBitmap());
}
public ScribbleWithBitmap()
{
Size size = SystemInformation.PrimaryMonitorMaximizedWindowSize;
bitmap = new Bitmap(size.Width, size.Height);
grfxBm = Graphics.FromImage(bitmap);
grfxBm.Clear(BackColor);
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if (mea.Button != MouseButtons.Left)
return;
ptLast = new Point(mea.X, mea.Y);
bTracking = true;
}
protected override void OnMouseMove(MouseEventArgs mea)
{
if (!bTracking)
return;
Point ptNew = new Point(mea.X, mea.Y);
Pen pen = new Pen(ForeColor);
Graphics grfx = CreateGraphics();
grfx.DrawLine(pen, ptLast, ptNew);
grfx.Dispose();
grfxBm.DrawLine(pen, ptLast, ptNew);
ptLast = ptNew;
}
protected override void OnMouseUp(MouseEventArgs mea)
{
bTracking = false;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
grfx.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
}
}
Set image resolution and paint it
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();
}
}
Shear Image
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);
Point[] destinationPoints = {
new Point(0, 0), // destination for upper-left point of original
new Point(300, 0), // destination for upper-right point of original
new Point(100, 300)};// destination for lower-left point of original
g.DrawImage(bmp, destinationPoints);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Shrink Image
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);
Rectangle sr = new Rectangle(80, 60, 400, 400);
Rectangle dr = new Rectangle(0, 0, 200, 200);
g.DrawImage(bmp, dr, sr, GraphicsUnit.Pixel);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Thumbnail Image
using System;
using System.Drawing;
using System.Windows.Forms;
class Thumbnail: Form
{
const int iSquare = 64;
Image imageThumbnail;
public static void Main()
{
Application.Run(new Thumbnail());
}
public Thumbnail()
{
Text = "Thumbnail";
ResizeRedraw = true;
Image image = Image.FromFile("Color.jpg");
int cxThumbnail, cyThumbnail;
if (image.Width > image.Height)
{
cxThumbnail = iSquare;
cyThumbnail = iSquare * image.Height / image.Width;
}
else
{
cyThumbnail = iSquare;
cxThumbnail = iSquare * image.Width / image.Height;
}
imageThumbnail = image.GetThumbnailImage(cxThumbnail, cyThumbnail,
null, (IntPtr) 0);
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
for (int y = 0; y < cy; y += iSquare){
for (int x = 0; x < cx; x += iSquare)
grfx.DrawImage(imageThumbnail,
x + (iSquare - imageThumbnail.Width) / 2,
y + (iSquare - imageThumbnail.Height) / 2,
imageThumbnail.Width, imageThumbnail.Height);
}
}
}