Csharp/C Sharp/2D Graphics/Icon Image
Содержание
Control Palette
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
/// <summary>
/// Summary description for ControlPalette.
/// </summary>
public class ControlPalette : System.Windows.Forms.Form
{
internal System.Windows.Forms.ImageList images;
internal System.Windows.Forms.Label lblPictureThree;
internal System.Windows.Forms.Label lblPictureTwo;
internal System.Windows.Forms.Label lblPictureOne;
private System.ruponentModel.IContainer components;
public ControlPalette()
{
//
// 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()
{
this.ruponents = new System.ruponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ControlPalette));
this.images = new System.Windows.Forms.ImageList(this.ruponents);
this.lblPictureThree = new System.Windows.Forms.Label();
this.lblPictureTwo = new System.Windows.Forms.Label();
this.lblPictureOne = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// images
//
this.images.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.images.ImageSize = new System.Drawing.Size(16, 16);
this.images.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("images.ImageStream")));
this.images.TransparentColor = System.Drawing.Color.Transparent;
//
// lblPictureThree
//
this.lblPictureThree.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPictureThree.Image = ((System.Drawing.Bitmap)(resources.GetObject("lblPictureThree.Image")));
this.lblPictureThree.ImageIndex = 2;
this.lblPictureThree.ImageList = this.images;
this.lblPictureThree.Location = new System.Drawing.Point(8, 112);
this.lblPictureThree.Name = "lblPictureThree";
this.lblPictureThree.Size = new System.Drawing.Size(56, 48);
this.lblPictureThree.TabIndex = 3;
this.lblPictureThree.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
//
// lblPictureTwo
//
this.lblPictureTwo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPictureTwo.Image = ((System.Drawing.Bitmap)(resources.GetObject("lblPictureTwo.Image")));
this.lblPictureTwo.ImageIndex = 1;
this.lblPictureTwo.ImageList = this.images;
this.lblPictureTwo.Location = new System.Drawing.Point(8, 60);
this.lblPictureTwo.Name = "lblPictureTwo";
this.lblPictureTwo.Size = new System.Drawing.Size(56, 48);
this.lblPictureTwo.TabIndex = 2;
this.lblPictureTwo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
//
// lblPictureOne
//
this.lblPictureOne.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblPictureOne.Image = ((System.Drawing.Bitmap)(resources.GetObject("lblPictureOne.Image")));
this.lblPictureOne.ImageIndex = 0;
this.lblPictureOne.ImageList = this.images;
this.lblPictureOne.Location = new System.Drawing.Point(8, 8);
this.lblPictureOne.Name = "lblPictureOne";
this.lblPictureOne.Size = new System.Drawing.Size(56, 48);
this.lblPictureOne.TabIndex = 1;
this.lblPictureOne.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
//
// ControlPalette
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(76, 174);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblPictureThree,
this.lblPictureTwo,
this.lblPictureOne});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Name = "ControlPalette";
this.Text = "Pictures";
this.ResumeLayout(false);
}
#endregion
private void lbl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Label lbl = (Label)sender;
lbl.DoDragDrop(lbl.Image, DragDropEffects.Copy);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ControlPalette());
}
}
Cube Image
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace Cube
{
/// <summary>
/// Summary description for CubeImage.
/// </summary>
public class CubeImage : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public CubeImage()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "Picture Cube";
//
// 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()
{
//
// CubeImage
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 189);
this.Name = "CubeImage";
this.Text = "CubeImage";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new CubeImage());
}
protected override void OnPaint(PaintEventArgs pea)
{
CubeDBuf();
}
private void CubeDBuf()
{
Graphics g;
string path;
int x = 100, y = 40;
Point A = new Point( 10, 50);
Point B = new Point(180, 50);
Point C = new Point( 10, 170);
Point a = new Point(A.X + x, A.Y - y);
Point b = new Point(B.X + x, B.Y - y);
Point Z = new Point(B.X, C.Y);
Point[] p3Fro = {A, B, C};
Point[] p3Top = {a, b, A};
Point[] p3Rig = {B, b, Z};
Bitmap bm = new Bitmap(B.X +x, C.Y + y);
g = Graphics.FromImage(bm);
path = @"IndonHouses.bmp";
Image im1 = Image.FromFile(path);
g.DrawImage(im1, p3Fro);
path = @"Pyramids.BMP";
Image im3 = Image.FromFile(path);
g.DrawImage(im3, p3Top);
path = @"TadjMahal.bmp";
Image im2 = Image.FromFile(path);
g.DrawImage(im2, p3Rig);
g = Graphics.FromHwnd(this.Handle);
g.DrawImage(bm, 1, 1);
g.Dispose();
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/P14_ImageCube.zip">P14_ImageCube.zip( 317 k)</a>
Draw image
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
// Change the path(s) if needed. If you have VS.NET write:
// "..\\..\\Altamira5.bmp" or @"..\..\Altamira5.bmp"
// otherwise
// "Altamira5.bmp"
// and compile with:
// csc Altamira.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Altamira
{
public class Altamira1 : Form
{
Pen p;
SolidBrush b, bT = new SolidBrush(Color.Black);
string path = "Altamira5.bmp"; // change the path if needed
Image im;
Font f;
public Altamira1()
{
InitializeComponent();
MyIni();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new System.Drawing.Size(290, 260);
this.Text = "Altamira";
this.ResumeLayout(false);
}
private void MyIni()
{
Color cP = Color.Gray;
Color cB = Color.LightGray;
p = new Pen(cP, 6);
b = new SolidBrush(cB);
im = Image.FromFile(path);
f = new Font(new FontFamily("Times New Roman"), 10);
}
static void Main()
{
Application.Run(new Altamira1());
}
protected override void OnPaint(PaintEventArgs pea)
{
Sketch();
//SketchDBuf();
}
private void Sketch()
{
Graphics g = Graphics.FromHwnd(this.Handle); // <=> g = CreateGraphics();
g.FillRectangle(b, 4, 4, 260, 220); // passe-partout
g.DrawRectangle(p, 4, 4, 260, 220); // frame
g.DrawImage(im, 33, 35, 200, 145 ); // image
g.DrawString("ALTAMIRA", f, bT, 180, 190); // text
g.Dispose();
}
private void SketchDBuf()
{
int hh = 3, w = 260, h = 220;
Graphics g;
Bitmap bm = new Bitmap(w + 2*hh, h + 2*hh);
g = Graphics.FromImage(bm); // buffer graphics
g.FillRectangle(b, hh , hh, w, h); // passe-partout
g.DrawRectangle(new Pen(Color.Gray, 2*hh), hh, hh, w, h); // frame
g.DrawImage(im, hh + 30, hh + 32, 200, 145); // image
g.DrawString("ALTAMIRA", f, bT, 180, 190); // text
g = Graphics.FromHwnd(this.Handle); // real graphics
g.DrawImage(bm, 1, 1);
g.Dispose();
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/P01_Altamira.zip">P01_Altamira.zip( 109 k)</a>
Icon Image Draw
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace IconImageDraw_c
{
/// <summary>
/// Summary description for IconImageDraw.
/// </summary>
public class IconImageDraw : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public IconImageDraw()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.MouseMove += new MouseEventHandler(this.DrawBox);
this.MouseDown += new MouseEventHandler(this.StartBox);
this.MouseUp += new MouseEventHandler(this.EndBox);
}
/// <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()
{
//
// IconImageDraw
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(492, 373);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MinimizeBox = false;
this.Name = "IconImageDraw";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "IconImageDraw";
this.Load += new System.EventHandler(this.IconImageDraw_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
///
//"R" holds the image, "Box" is the new image home currently being
//drawn by holding the mouse down and dragging
private Rectangle R = Rectangle.Empty;
private Rectangle Box = Rectangle.Empty;
private Image I = Image.FromFile("sample.jpg");
private Icon ThisIcon = new Icon("usa.ico");
private bool OK2Paint = false;
[STAThread]
static void Main()
{
Application.Run(new IconImageDraw());
}
private void IconImageDraw_Load(object sender, System.EventArgs e)
{
}
protected override void OnPaint ( PaintEventArgs e )
{
//Always draw the icon
e.Graphics.DrawIcon(ThisIcon, 1, 1);
//Bail if rectangle is empty
if ( R == Rectangle.Empty )
return;
if ( !OK2Paint )
return;
Pen P = new Pen(Brushes.Black, 3);
e.Graphics.DrawRectangle(P, R);
// Draw image based on rectangle.
e.Graphics.DrawImage(I, R);
P.Dispose();
}
private void DrawBox ( System.Object sender , MouseEventArgs m )
{
//Prints the x,y coordinates directly on the screen
Graphics G = this.CreateGraphics();
Rectangle TextR = new Rectangle(10, this.Height-50, 100, 20 );
SolidBrush B = new SolidBrush(this.BackColor);
G.FillRectangle(B, TextR);
G.DrawString ( m.X.ToString() + ", " + m.Y.ToString(),
new Font("Arial", 10),
Brushes.Black, TextR, StringFormat.GenericDefault );
B.Dispose();
//Draw the box as the mouse drags
if ( m.Button == MouseButtons.Left )
{
if ( Box != Rectangle.Empty )
{
Pen P = new Pen(new SolidBrush(this.BackColor),1);
G.DrawRectangle ( P, Box );
P.Dispose();
}
Box = new Rectangle ( R.X, R.Y, m.X - R.X, m.Y - R.Y );
G.DrawRectangle( Pens.Black, Box );
}
}
private void StartBox( System.Object sender , MouseEventArgs m )
{
if ( m.Button == MouseButtons.Left )
{
R.X=m.X;
R.Y=m.Y;
OK2Paint = false;
}
}
private void EndBox( System.Object sender , MouseEventArgs m )
{
R.Width = m.X - R.X;
R.Height = m.Y - R.Y;
OK2Paint = true;
this.Refresh();
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/IconImageDraw-c.zip">IconImageDraw-c.zip( 12 k)</a>
Image Class
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
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 ImageClass
{
/// <summary>
/// Summary description for ImageClass.
/// </summary>
public class ImageClass : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog ofdBitmap;
private System.Windows.Forms.Button cmdOpen;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label lblHT;
private System.Windows.Forms.Label lblWidth;
private System.Windows.Forms.Label lblHdpi;
private System.Windows.Forms.Label lblVdpi;
private System.Windows.Forms.Label lblFormat;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label lblSize;
private System.Windows.Forms.Label lblDIM;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Button cmdSetRes;
#region Class local variables
Bitmap Bmp;
RectangleF BMPContainer;
#endregion
public ImageClass()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
ofdBitmap.Filter= "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
ofdBitmap.InitialDirectory = "";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
if (Bmp != null)
Bmp.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()
{
this.ofdBitmap = new System.Windows.Forms.OpenFileDialog();
this.cmdOpen = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.lblHT = new System.Windows.Forms.Label();
this.lblWidth = new System.Windows.Forms.Label();
this.lblHdpi = new System.Windows.Forms.Label();
this.lblVdpi = new System.Windows.Forms.Label();
this.lblFormat = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.lblSize = new System.Windows.Forms.Label();
this.lblDIM = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.cmdSetRes = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// ofdBitmap
//
this.ofdBitmap.FileOk += new System.ruponentModel.CancelEventHandler(this.OpenFile);
//
// cmdOpen
//
this.cmdOpen.Location = new System.Drawing.Point(16, 320);
this.cmdOpen.Name = "cmdOpen";
this.cmdOpen.Size = new System.Drawing.Size(72, 32);
this.cmdOpen.TabIndex = 0;
this.cmdOpen.Text = "Open";
this.cmdOpen.Click += new System.EventHandler(this.Open);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblDIM,
this.label8,
this.lblSize,
this.label6,
this.lblFormat,
this.lblVdpi,
this.lblHdpi,
this.lblWidth,
this.lblHT,
this.label5,
this.label4,
this.label3,
this.label2,
this.label1});
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(144, 80);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(224, 224);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Attributes";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Height";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Hdpi";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 48);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 16);
this.label3.TabIndex = 2;
this.label3.Text = "Width";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 96);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 16);
this.label4.TabIndex = 3;
this.label4.Text = "Vdpi";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 120);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 16);
this.label5.TabIndex = 4;
this.label5.Text = "Format";
//
// lblHT
//
this.lblHT.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblHT.Location = new System.Drawing.Point(80, 24);
this.lblHT.Name = "lblHT";
this.lblHT.Size = new System.Drawing.Size(136, 16);
this.lblHT.TabIndex = 5;
//
// lblWidth
//
this.lblWidth.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblWidth.Location = new System.Drawing.Point(80, 48);
this.lblWidth.Name = "lblWidth";
this.lblWidth.Size = new System.Drawing.Size(136, 16);
this.lblWidth.TabIndex = 6;
//
// lblHdpi
//
this.lblHdpi.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblHdpi.Location = new System.Drawing.Point(80, 72);
this.lblHdpi.Name = "lblHdpi";
this.lblHdpi.Size = new System.Drawing.Size(136, 16);
this.lblHdpi.TabIndex = 7;
//
// lblVdpi
//
this.lblVdpi.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblVdpi.Location = new System.Drawing.Point(80, 96);
this.lblVdpi.Name = "lblVdpi";
this.lblVdpi.Size = new System.Drawing.Size(136, 16);
this.lblVdpi.TabIndex = 8;
//
// lblFormat
//
this.lblFormat.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblFormat.Location = new System.Drawing.Point(80, 120);
this.lblFormat.Name = "lblFormat";
this.lblFormat.Size = new System.Drawing.Size(136, 16);
this.lblFormat.TabIndex = 9;
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 160);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(64, 16);
this.label6.TabIndex = 10;
this.label6.Text = "Size";
//
// lblSize
//
this.lblSize.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblSize.Location = new System.Drawing.Point(80, 160);
this.lblSize.Name = "lblSize";
this.lblSize.Size = new System.Drawing.Size(136, 16);
this.lblSize.TabIndex = 11;
//
// lblDIM
//
this.lblDIM.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblDIM.Location = new System.Drawing.Point(80, 184);
this.lblDIM.Name = "lblDIM";
this.lblDIM.Size = new System.Drawing.Size(136, 16);
this.lblDIM.TabIndex = 13;
//
// label8
//
this.label8.Location = new System.Drawing.Point(8, 184);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(64, 16);
this.label8.TabIndex = 12;
this.label8.Text = "Dimensions";
//
// cmdSetRes
//
this.cmdSetRes.Location = new System.Drawing.Point(144, 320);
this.cmdSetRes.Name = "cmdSetRes";
this.cmdSetRes.Size = new System.Drawing.Size(128, 32);
this.cmdSetRes.TabIndex = 3;
this.cmdSetRes.Text = "Reset Resolution";
this.cmdSetRes.Click += new System.EventHandler(this.SetRes);
//
// ImageClass
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdSetRes,
this.groupBox1,
this.cmdOpen});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ImageClass";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ImageClass";
this.Load += new System.EventHandler(this.ImageClass_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ImageClass());
}
private void ImageClass_Load(object sender, System.EventArgs e)
{
}
protected override void OnPaint( PaintEventArgs e)
{
Graphics G = e.Graphics;
if ( Bmp != null )
{
//OK Folks. This is a BitBlt!
G.DrawImage(Bmp, BMPContainer);
}
base.OnPaint(e);
}
private void Open(object sender, System.EventArgs e)
{
ofdBitmap.ShowDialog();
}
private void OpenFile(object sender,
System.ruponentModel.CancelEventArgs e)
{
//Wondering what sender is? Use reflection!
if ( !( sender is System.Windows.Forms.OpenFileDialog ) )
return;
Bmp = new Bitmap( ofdBitmap.FileName );
if ( Bmp != null )
{
ShowStats();
DrawBMP();
}
}
private void SetRes(object sender, System.EventArgs e)
{
if ( Bmp != null )
{
Bmp.SetResolution(150, 150);
DrawBMP();
}
}
private void DrawBMP()
{
if ( Bmp != null )
{
//Invalidate only the area where the image will be drawn
Graphics G = this.CreateGraphics();
GraphicsUnit GU = G.PageUnit;
BMPContainer = Bmp.GetBounds( ref GU ); //X,Y = 0
ShowStats();
this.Invalidate( Rectangle.Round( BMPContainer ) );
G.Dispose();
}
}
private void ShowStats()
{
if ( Bmp != null )
{
//Show some properties here
lblHT.Text = Bmp.Height.ToString();
lblWidth.Text = Bmp.Width.ToString();
lblVdpi.Text = Bmp.VerticalResolution.ToString();
lblHdpi.Text = Bmp.HorizontalResolution.ToString();
lblFormat.Text = Bmp.PixelFormat.ToString();
lblSize.Text = Bmp.Size.ToString();
lblDIM.Text = Bmp.PhysicalDimension.ToString();
}
}
}
}
Image Flip
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace ImageFlip
{
/// <summary>
/// Summary description for ImageFlip.
/// </summary>
public class ImageFlip : 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;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public ImageFlip()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "Flipping and Rotating";
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);
//
// 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()
{
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();
//
// groupBox1
//
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.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(248, 80);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(120, 16);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(112, 24);
this.radioButton1.TabIndex = 1;
this.radioButton1.Text = "radioButton1";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(16, 16);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(304, 200);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(16, 48);
this.radioButton2.Name = "radioButton2";
this.radioButton2.TabIndex = 2;
this.radioButton2.Text = "radioButton2";
//
// radioButton3
//
this.radioButton3.Location = new System.Drawing.Point(120, 48);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(120, 24);
this.radioButton3.TabIndex = 3;
this.radioButton3.Text = "radioButton3";
//
// ImageFlip
//
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.Name = "ImageFlip";
this.Text = "ImageFlip";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ImageFlip());
}
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); // For text "Himalaya"
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) // Rotate180FlipY
{
im2.RotateFlip(RotateFlipType.Rotate180FlipY);
g.DrawImage(im2, w2, 0, w2, h2);
}
else g.FillRectangle(b, w2, 0, w2, h2); // Clear old
if(this.radioButton2.Checked) // Rotate180FlipX
{
im2.RotateFlip(RotateFlipType.Rotate180FlipX);
g.DrawImage(im2, 0, h2, w2, h2);
}
else g.FillRectangle(b, 0, h2, w2, h2); // Clear old
if(this.radioButton3.Checked) // Rotate180FlipNone
{
im2.RotateFlip(RotateFlipType.Rotate180FlipNone);
g.DrawImage(im2, w2, h2, w2, h2); // Clear old
}
else g.FillRectangle(b, w2, h2, w2, h2);
im2.Dispose();
}
else Clear(g);
b.Dispose(); g.Dispose();
}
protected void ReadImage()
{
string path = @"Himalaya.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;
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/P16_RotateFlip.zip">P16_RotateFlip.zip( 240 k)</a>
Image Icon Form
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
namespace ImageIcon
{
/// <summary>
/// Summary description for ImageIconForm.
/// </summary>
public class ImageIconForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public ImageIconForm()
{
//
// 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()
{
//
// ImageIconForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "ImageIconForm";
this.Text = "ImageIconForm";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ImageIconForm());
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
// Icon 3 times:
string p = @"C:\Program Files\Microsoft Visual Studio .NET\VC#\CSharpProjectItems\icon.ico";
Icon ic = new Icon(p);
this.Icon = ic; // Icon 1)
Graphics g = e.Graphics;
g.DrawIcon(ic, 0, 0); // Icon 2)
Image i = ic.ToBitmap();
g.DrawImage(i, 50, 0); // Icon 3)
// Thumbnail:
p = @"MonetZsu.JPG";
i = Image.FromFile(p);
Image tn = i.GetThumbnailImage(50, 50, null, IntPtr.Zero); // <=>(IntPtr)0
g.DrawImage(tn, 100, 0, tn.Width, tn.Height);
// Picture from Internet:
p = "http://www.kosice.sk/newimg/kolazx4.jpg";
WebRequest wReq = WebRequest.Create(p); // using System.Net;
WebResponse wRes = wReq.GetResponse();
Stream strm = wRes.GetResponseStream(); // using System.IO;
Image im = Image.FromStream(strm);
g.DrawImage(im, 0, 50);
strm.Close();
g.Dispose();
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/P19_WebIconThumbNail.zip">P19_WebIconThumbNail.zip( 19 k)</a>
Image Paint Simple Demo
/*
Code revised from chapter 6
GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward
Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604
Sample chapter
http://www.packtpub.ru/files/1604_CustomControls_SampleChapter.pdf
For More info on GDI+ Custom Control with Microsoft Visual C# book
visit website www.packtpub.ru
*/
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace WarpControlApp1
{
public partial class Form1 : Form
{
Image img = null;
public Form1()
{
InitializeComponent();
}
private Image CreatePicture()
{
// Create a new Bitmap object, 50 x 50 pixels in size
Image canvas = new Bitmap(50, 50);
// create an object that will do the drawing operations
Graphics artist = Graphics.FromImage(canvas);
// draw a few shapes on the canvas picture
artist.Clear(Color.Lime);
artist.FillEllipse(Brushes.Red, 3, 30, 30, 30);
artist.DrawBezier(new Pen(Color.Blue, 3), 0, 0, 40, 15, 10, 35, 50, 50);
// now the drawing is done, we can discard the artist object
artist.Dispose();
//return the picture
return canvas;
}
private void Form1_Load(object sender, EventArgs e)
{
img = CreatePicture();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Random rand = new Random(); // randomises our drawing parameters
// set up all our parameters first before calling DrawWarpedPicture.
Graphics target = this.CreateGraphics(); // draw onto the form"s surface
PointF pivotOnImage = new PointF(img.Width / 2, img.Height / 2);
PointF pivotOnTarget = new PointF((Single)e.X, (Single)e.Y);
double rotate = rand.NextDouble() * 360;
double scaleFactor = 0.2 + (rand.NextDouble() * 2);
SizeF skewing = new SizeF(rand.Next(-20, 21), rand.Next(-20, 21));
// draw it!
ImageWarper warper = new ImageWarper();
warper.DrawWarpedPicture(target, img, pivotOnImage, pivotOnTarget, rotate, scaleFactor, skewing);
}
private System.ruponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "";
this.Text = "Click the form to draw";
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public class ImageWarper : Component
{
public void DrawWarpedPicture(
Graphics surface, // the surface to draw on
Image img, // the image to draw
PointF sourceAxle, // pivot point passing through image.
PointF destAxle, // pivot point"s position on destination surface
double degrees, // degrees through which the image is rotated clockwise
double scale, // size multiplier
SizeF skew // the slanting effect size, applies BEFORE scaling or rotation
)
{
// give this array temporary coords that will be overwritten in the loop below
// the skewing is done here orthogonally, before any trigonometry is applied
PointF[] temp = new PointF[3] {
new PointF(skew.Width, -skew.Height),
new PointF((img.Width - 1) + skew.Width, skew.Height),
new PointF(-skew.Width,(img.Height - 1) - skew.Height) };
double ang, dist;
double radians = degrees * (Math.PI / 180);
// convert the images corner points into scaled, rotated, skewed and translated points
for (int i = 0; i < 3; i++)
{
// measure the angle to the image"s corner and then add the rotation value to it
ang = GetBearingRadians(sourceAxle, temp[i], out dist) + radians;
dist *= scale; // scale
temp[i] = new PointF((Single)((Math.Cos(ang) * dist) + destAxle.X), (Single)((Math.Sin(ang) * dist) + destAxle.Y));
}
surface.DrawImage(img, temp);
}
private double GetBearingRadians(PointF reference, PointF target, out double distance)
{
double dx = target.X - reference.X;
double dy = target.Y - reference.Y;
double result = Math.Atan2(dy, dx);
distance = Math.Sqrt((dx * dx) + (dy * dy));
if (result < 0)
result += (Math.PI * 2); // add the negative number to 360 degrees to correct the atan2 value
return result;
}
}
}
Image Warper App
/*
Code revised from chapter 6
GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward
Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604
Sample chapter
http://www.packtpub.ru/files/1604_CustomControls_SampleChapter.pdf
For More info on GDI+ Custom Control with Microsoft Visual C# book
visit website www.packtpub.ru
*/
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ImageWarperApp
{
public partial class Form1 : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
}
private void applyButton_Click(object sender, EventArgs e)
{
imageWarperControl1.ImageAngle = Double.Parse(angleBox.Text) * System.Math.PI / 180;
imageWarperControl1.ImageScale = Double.Parse(scaleBox.Text) / 100;
imageWarperControl1.ImageSkew = new SizeF(float.Parse(skewHorizontalBox.Text), float.Parse(skewVerticalBox.Text));
}
private System.ruponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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()
{
this.angleBox = new System.Windows.Forms.TextBox();
this.scaleBox = new System.Windows.Forms.TextBox();
this.skewHorizontalBox = new System.Windows.Forms.TextBox();
this.skewVerticalBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.applyButton = new System.Windows.Forms.Button();
this.imageWarperControl1 = new ImageWarperApp.ImageWarperControl();
this.SuspendLayout();
//
// angleBox
//
this.angleBox.Location = new System.Drawing.Point(128, 42);
this.angleBox.Name = "angleBox";
this.angleBox.Size = new System.Drawing.Size(100, 20);
this.angleBox.TabIndex = 1;
//
// scaleBox
//
this.scaleBox.Location = new System.Drawing.Point(128, 100);
this.scaleBox.Name = "scaleBox";
this.scaleBox.Size = new System.Drawing.Size(100, 20);
this.scaleBox.TabIndex = 2;
//
// skewHorizontalBox
//
this.skewHorizontalBox.Location = new System.Drawing.Point(128, 157);
this.skewHorizontalBox.Name = "skewHorizontalBox";
this.skewHorizontalBox.Size = new System.Drawing.Size(100, 20);
this.skewHorizontalBox.TabIndex = 3;
//
// skewVerticalBox
//
this.skewVerticalBox.Location = new System.Drawing.Point(128, 217);
this.skewVerticalBox.Name = "skewVerticalBox";
this.skewVerticalBox.Size = new System.Drawing.Size(100, 20);
this.skewVerticalBox.TabIndex = 4;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(7, 45);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 13);
this.label1.TabIndex = 5;
this.label1.Text = "Rotation Angle (deg)";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(7, 103);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(84, 13);
this.label2.TabIndex = 6;
this.label2.Text = "Scale Factor (%)";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(7, 160);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(117, 13);
this.label3.TabIndex = 7;
this.label3.Text = "Skew Horizontal Factor";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(7, 220);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(105, 13);
this.label4.TabIndex = 8;
this.label4.Text = "Skew Vertical Factor";
//
// applyButton
//
this.applyButton.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.applyButton.Location = new System.Drawing.Point(61, 285);
this.applyButton.Name = "applyButton";
this.applyButton.Size = new System.Drawing.Size(118, 34);
this.applyButton.TabIndex = 9;
this.applyButton.Text = "Apply New Settings";
this.applyButton.UseVisualStyleBackColor = false;
this.applyButton.Click += new System.EventHandler(this.applyButton_Click);
//
// imageWarperControl1
//
this.imageWarperControl1.ImageAngle = 0;
this.imageWarperControl1.ImageScale = 0;
this.imageWarperControl1.ImageSkew = new System.Drawing.SizeF(0F, 0F);
this.imageWarperControl1.Location = new System.Drawing.Point(253, 45);
this.imageWarperControl1.Name = "imageWarperControl1";
this.imageWarperControl1.Size = new System.Drawing.Size(185, 192);
this.imageWarperControl1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(450, 342);
this.Controls.Add(this.applyButton);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.skewVerticalBox);
this.Controls.Add(this.skewHorizontalBox);
this.Controls.Add(this.scaleBox);
this.Controls.Add(this.angleBox);
this.Controls.Add(this.imageWarperControl1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private ImageWarperControl imageWarperControl1;
private System.Windows.Forms.TextBox angleBox;
private System.Windows.Forms.TextBox scaleBox;
private System.Windows.Forms.TextBox skewHorizontalBox;
private System.Windows.Forms.TextBox skewVerticalBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button applyButton;
}
public partial class ImageWarperControl : UserControl
{
public ImageWarperControl()
{
InitializeComponent();
}
private void ImageWarperControl_Load(object sender, EventArgs e)
{
img = CreatePicture();
}
private void ImageWarperControl_Paint(object sender, PaintEventArgs e)
{
// set up all our parameters first before calling DrawWarpedPicture.
Graphics target = this.CreateGraphics(); //draw onto the form"s surface
PointF pivotOnImage = new PointF(img.Width / 2, img.Height / 2);
PointF pivotOnTarget = new PointF(this.Width / 2, this.Height / 2);
double rotate = imageAngle;
double scaleFactor = imageScale;
SizeF skewing = imageSkew;
DrawWarpedPicture(target, img, pivotOnImage, pivotOnTarget, rotate, scaleFactor, skewing);
}
private Image CreatePicture()
{
// Create a new Bitmap object, 50 x 50 pixels in size
Image canvas = new Bitmap(50, 50);
// create an object that will do the drawing operations
Graphics artist = Graphics.FromImage(canvas);
// draw a few shapes on the canvas picture
artist.Clear(Color.Lime);
artist.FillEllipse(Brushes.Red, 3, 30, 30, 30);
artist.DrawBezier(new Pen(Color.Blue, 3), 0, 0, 40, 15, 10, 35, 50, 50);
// now the drawing is done, we can discard the artist object
artist.Dispose();
// return the picture
return canvas;
}
public void DrawWarpedPicture(
Graphics surface, //the surface to draw on
Image img, //the image to draw
PointF sourceAxle, //pivot point passing through image.
PointF destAxle, //pivot point"s position on destination surface
double degrees, //degrees through which the image is rotated clockwise
double scale, //size multiplier
SizeF skew //the slanting effect size, applies BEFORE scaling or rotation
)
{
//give this array temporary coords that will be overwritten in the loop below
//the skewing is done here orthogonally, before any trigonometry is applied
PointF[] temp = new PointF[3] { new PointF(skew.Width, -skew.Height),
new PointF((img.Width - 1) + skew.Width, skew.Height),
new PointF(-skew.Width,(img.Height - 1) - skew.Height) };
double ang, dist;
//convert the images corner points into scaled, rotated, skewed and translated points
for (int i = 0; i < 3; i++)
{
//measure the angle to the image"s corner and then add the rotation value to it
ang = GetBearingRadians(sourceAxle, temp[i], out dist) + degrees;
dist *= scale; //scale
temp[i] = new PointF((Single)((Math.Cos(ang) * dist) + destAxle.X), (Single)((Math.Sin(ang) * dist) + destAxle.Y));
}
surface.DrawImage(img, temp);
}
private static double GetBearingRadians(PointF reference, PointF target, out double distance)
{
double dx = target.X - reference.X;
double dy = target.Y - reference.Y;
double result = Math.Atan2(dy, dx);
distance = Math.Sqrt((dx * dx) + (dy * dy));
if (result < 0)
result += (Math.PI * 2); //add the negative number to 360 degrees to correct the atan2 value
return result;
}
private double imageAngle;
private double imageScale;
private SizeF imageSkew;
private Image img = null;
public double ImageAngle
{
get
{
return imageAngle;
}
set
{
if (imageAngle != value)
{
imageAngle = value;
Invalidate();
}
}
}
public double ImageScale
{
get
{
return imageScale;
}
set
{
if (imageScale != value)
{
imageScale = value;
Invalidate();
}
}
}
public SizeF ImageSkew
{
get
{
return imageSkew;
}
set
{
if (imageSkew != value)
{
imageSkew = value;
Invalidate();
}
}
}
private System.ruponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component 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()
{
this.SuspendLayout();
//
// ImageWarperControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Name = "ImageWarperControl";
this.Load += new System.EventHandler(this.ImageWarperControl_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.ImageWarperControl_Paint);
this.ResumeLayout(false);
}
#endregion
}
}
Image Zoom 1
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace ImageZoom
{
/// <summary>
/// Summary description for ImageZoom.
/// </summary>
public class ImageZoom1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.CheckBox checkBox1;
Image im = null;
Image im2 = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public ImageZoom1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
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);
//
// 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()
{
this.label1 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.label2 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 240);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// groupBox1
//
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.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(72, 128);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(8, 32);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(56, 24);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(8, 64);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(16, 24);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "radioButton1";
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(40, 64);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(16, 24);
this.radioButton2.TabIndex = 0;
this.radioButton2.Text = "radioButton1";
//
// radioButton3
//
this.radioButton3.Location = new System.Drawing.Point(8, 96);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(16, 24);
this.radioButton3.TabIndex = 0;
this.radioButton3.Text = "radioButton1";
//
// radioButton4
//
this.radioButton4.Location = new System.Drawing.Point(40, 96);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(16, 24);
this.radioButton4.TabIndex = 0;
this.radioButton4.Text = "radioButton1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(328, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(200, 240);
this.label2.TabIndex = 0;
this.label2.Text = "label1";
//
// ImageZoom
//
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.Name = "ImageZoom";
this.Text = "ImageZoom";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ImageZoom1());
}
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);
// Center part:
recPart = new Rectangle(im.Width/4, im.Height/4, im.Width/2,
im.Height/2);
if(this.radioButton1.Checked) // Left-Top part
recPart = new Rectangle(0, 0, im.Width/2, im.Height/2);
if(this.radioButton2.Checked) // Right-Top part
recPart = new Rectangle(im.Width/2, 0, im.Width/2, im.Height/2);
if(this.radioButton3.Checked) // Left-Down part
recPart = new Rectangle(0, im.Height/2, im.Width/2, im.Height/2);
if(this.radioButton4.Checked) // Right-Down part
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 = @"szeret3.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;
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/P15_ImageZoom.zip">P15_ImageZoom.zip( 397 k)</a>
MetaFile and Draw
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
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 MetaFile_c
{
/// <summary>
/// Summary description for MetaFile.
/// </summary>
public class MetaFile : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public MetaFile()
{
//
// 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()
{
//
// MetaFile
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MetaFile";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "MetaFile";
this.Load += new System.EventHandler(this.MetaFile_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MetaFile());
}
private void MetaFile_Load(object sender, System.EventArgs e)
{
// Create a graphics object from the forms graphics object
// and get the handle to it.
using (Graphics FormGraphics = this.CreateGraphics())
{
IntPtr hdc = FormGraphics.GetHdc();
// Now create a blank metafile using the graphics handle
// This is the metafile where all drawing instructions will
// be recorded.
Metafile mf = new Metafile("mymeta.emf", hdc);
// In order to record drawing commands we need to draw on something
// That something is a new graphics object
using (Graphics MetaGraphics = Graphics.FromImage(mf))
{
using ( mf )
{
MetaGraphics.SmoothingMode = SmoothingMode.AntiAlias;
//Now we are ready to draw on the metagraphics object
MetaGraphics.DrawRectangle(Pens.Black, 10, 10, 100, 50 );
MetaGraphics.DrawLine(Pens.Orange, 10, 70, 150, 100 );
}
}
FormGraphics.ReleaseHdc(hdc);
}
}
protected override void OnPaint( PaintEventArgs e )
{
Metafile mf = new Metafile("mymeta.emf");
e.Graphics.DrawImage(mf, new Point(10,10));
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/MetaFile-c.zip">MetaFile-c.zip( 1 k)</a>
Picture Controls
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
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 PicControls
{
/// <summary>
/// Summary description for PicControls.
/// </summary>
public class PicControls : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox p;
private System.Windows.Forms.Panel PicPanel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public PicControls()
{
//
// 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()
{
this.p = new System.Windows.Forms.PictureBox();
this.PicPanel = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// p
//
this.p.Location = new System.Drawing.Point(32, 40);
this.p.Name = "p";
this.p.Size = new System.Drawing.Size(136, 104);
this.p.TabIndex = 0;
this.p.TabStop = false;
//
// PicPanel
//
this.PicPanel.Location = new System.Drawing.Point(200, 120);
this.PicPanel.Name = "PicPanel";
this.PicPanel.Size = new System.Drawing.Size(200, 184);
this.PicPanel.TabIndex = 1;
//
// PicControls
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.PicPanel,
this.p});
this.Name = "PicControls";
this.Text = "PicControls";
this.Load += new System.EventHandler(this.PicControls_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new PicControls());
}
private void PicControls_Load(object sender, System.EventArgs e)
{
Bitmap b = new Bitmap("crane.jpg");
//PictureBox is "p"
p.Image = (Image)b;
}
}
}
<A href="http://www.nfex.ru/Code/CSharpDownload/PicControls.zip">PicControls.zip( 7 k)</a>