实例介绍
【实例简介】c#版绘图工具
【实例截图】
【核心代码】
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using Painter.Common;
namespace Painter
{
/// <summary>
/// PaintForm 的摘要说明。
/// </summary>
public class PaintForm : System.Windows.Forms.Form
{
#region Windows组件定义
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.PictureBox myPicture;
private System.Windows.Forms.StatusBar myStatusBar;
private System.Windows.Forms.ToolBar MainToolBar;
private System.Windows.Forms.ToolBarButton tbb_Line;
private System.Windows.Forms.ToolBarButton tbb_Rect;
private System.Windows.Forms.ToolBarButton tbb_Circle;
private System.Windows.Forms.ToolBarButton tbb_Cursor;
private System.Windows.Forms.StatusBarPanel sbp_Info;
private System.Windows.Forms.StatusBarPanel sbp_Coord;
private System.Windows.Forms.MenuItem menu_File;
private System.Windows.Forms.MenuItem menu_FileNew;
private System.Windows.Forms.MenuItem menu_FileOpen;
private System.Windows.Forms.MenuItem menu_FileSave;
private System.Windows.Forms.MenuItem menu_FileSaveAs;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menu_FileExit;
private System.Windows.Forms.MenuItem menu_Edit;
private System.Windows.Forms.MenuItem menu_EditSize;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menu_Help;
private System.Windows.Forms.MenuItem menu_EditSelAll;
private System.Windows.Forms.MenuItem menu_HelpAbout;
private System.Windows.Forms.Panel myPanel;
private System.Windows.Forms.MenuItem menu_EditClear;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menu_EditPenColor;
private System.Windows.Forms.MenuItem menu_EditPenWidth;
private System.Windows.Forms.MenuItem menu_EditPenStyle;
#endregion
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private ArrayList Items;
private ClickState CursorState = ClickState.Free;
private int ptx_old,pty_old,ptx_new,pty_new,ptx_start,pty_start;
private int movex_old,movey_old,movex_new,movey_new,movex_start,movey_start;
private Color pencolor;
private float penwidth;
private System.Drawing.Drawing2D.DashStyle penstyle;
private bool IsSaved = false;
private string FileName,ImgFileName;
private int imgWidth = 800,imgHeight = 600;
private System.Windows.Forms.MenuItem menu_EditDelItem;
public PaintForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
Items = new ArrayList();
ptx_old = ptx_new = pty_old = pty_new = ptx_start = pty_start = 0;
movex_old = movex_new = movey_old = movey_new = movex_start = movey_start = 0;
pencolor = Color.Black;
penstyle = DashStyle.Solid;
penwidth = 1;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
#region 设计器自动生成代码
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new PaintForm());
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu = new System.Windows.Forms.MainMenu();
this.menu_File = new System.Windows.Forms.MenuItem();
this.menu_FileNew = new System.Windows.Forms.MenuItem();
this.menu_FileOpen = new System.Windows.Forms.MenuItem();
this.menu_FileSave = new System.Windows.Forms.MenuItem();
this.menu_FileSaveAs = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menu_FileExit = new System.Windows.Forms.MenuItem();
this.menu_Edit = new System.Windows.Forms.MenuItem();
this.menu_EditSize = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menu_EditPenColor = new System.Windows.Forms.MenuItem();
this.menu_EditPenWidth = new System.Windows.Forms.MenuItem();
this.menu_EditPenStyle = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menu_EditClear = new System.Windows.Forms.MenuItem();
this.menu_EditSelAll = new System.Windows.Forms.MenuItem();
this.menu_Help = new System.Windows.Forms.MenuItem();
this.menu_HelpAbout = new System.Windows.Forms.MenuItem();
this.myPicture = new System.Windows.Forms.PictureBox();
this.MainToolBar = new System.Windows.Forms.ToolBar();
this.tbb_Cursor = new System.Windows.Forms.ToolBarButton();
this.tbb_Line = new System.Windows.Forms.ToolBarButton();
this.tbb_Rect = new System.Windows.Forms.ToolBarButton();
this.tbb_Circle = new System.Windows.Forms.ToolBarButton();
this.myStatusBar = new System.Windows.Forms.StatusBar();
this.sbp_Info = new System.Windows.Forms.StatusBarPanel();
this.sbp_Coord = new System.Windows.Forms.StatusBarPanel();
this.myPanel = new System.Windows.Forms.Panel();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.menu_EditDelItem = new System.Windows.Forms.MenuItem();
((System.ComponentModel.ISupportInitialize)(this.sbp_Info)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.sbp_Coord)).BeginInit();
this.myPanel.SuspendLayout();
this.SuspendLayout();
//
// mainMenu
//
this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menu_File,
this.menu_Edit,
this.menu_Help});
//
// menu_File
//
this.menu_File.Index = 0;
this.menu_File.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menu_FileNew,
this.menu_FileOpen,
this.menu_FileSave,
this.menu_FileSaveAs,
this.menuItem8,
this.menu_FileExit});
this.menu_File.Text = "文件(&F)";
//
// menu_FileNew
//
this.menu_FileNew.Index = 0;
this.menu_FileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.menu_FileNew.Text = "新建";
this.menu_FileNew.Click = new System.EventHandler(this.menu_FileNew_Click);
//
// menu_FileOpen
//
this.menu_FileOpen.Index = 1;
this.menu_FileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
this.menu_FileOpen.Text = "打开";
this.menu_FileOpen.Click = new System.EventHandler(this.menu_FileOpen_Click);
//
// menu_FileSave
//
this.menu_FileSave.Index = 2;
this.menu_FileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
this.menu_FileSave.Text = "保存";
this.menu_FileSave.Click = new System.EventHandler(this.menu_FileSave_Click);
//
// menu_FileSaveAs
//
this.menu_FileSaveAs.Index = 3;
this.menu_FileSaveAs.Text = "另存为...";
this.menu_FileSaveAs.Click = new System.EventHandler(this.menu_FileSaveAs_Click);
//
// menuItem8
//
this.menuItem8.Index = 4;
this.menuItem8.Text = "-";
//
// menu_FileExit
//
this.menu_FileExit.Index = 5;
this.menu_FileExit.Text = "退出(&X)";
this.menu_FileExit.Click = new System.EventHandler(this.menu_FileExit_Click);
//
// menu_Edit
//
this.menu_Edit.Index = 1;
this.menu_Edit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menu_EditSize,
this.menuItem2,
this.menu_EditPenColor,
this.menu_EditPenWidth,
this.menu_EditPenStyle,
this.menuItem1,
this.menu_EditDelItem,
this.menu_EditClear,
this.menu_EditSelAll});
this.menu_Edit.Text = "编辑(&E)";
//
// menu_EditSize
//
this.menu_EditSize.Index = 0;
this.menu_EditSize.Text = "图像大小";
this.menu_EditSize.Click = new System.EventHandler(this.menu_EditSize_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "-";
//
// menu_EditPenColor
//
this.menu_EditPenColor.Index = 2;
this.menu_EditPenColor.Text = "选取画笔颜色";
this.menu_EditPenColor.Click = new System.EventHandler(this.menu_PenColor_Click);
//
// menu_EditPenWidth
//
this.menu_EditPenWidth.Index = 3;
this.menu_EditPenWidth.Text = "选取画笔粗度";
this.menu_EditPenWidth.Click = new System.EventHandler(this.menu_PenWidth_Click);
//
// menu_EditPenStyle
//
this.menu_EditPenStyle.Index = 4;
this.menu_EditPenStyle.Text = "选取画笔样式";
this.menu_EditPenStyle.Click = new System.EventHandler(this.menu_PenStyle_Click);
//
// menuItem1
//
this.menuItem1.Index = 5;
this.menuItem1.Text = "-";
//
// menu_EditClear
//
this.menu_EditClear.Index = 7;
this.menu_EditClear.Text = "清除";
this.menu_EditClear.Click = new System.EventHandler(this.menu_EditClear_Click);
//
// menu_EditSelAll
//
this.menu_EditSelAll.Index = 8;
this.menu_EditSelAll.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
this.menu_EditSelAll.Text = "全选";
this.menu_EditSelAll.Click = new System.EventHandler(this.menu_EditSelAll_Click);
//
// menu_Help
//
this.menu_Help.Index = 2;
this.menu_Help.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menu_HelpAbout});
this.menu_Help.Text = "帮助(&H)";
//
// menu_HelpAbout
//
this.menu_HelpAbout.Index = 0;
this.menu_HelpAbout.Text = "关于";
this.menu_HelpAbout.Click = new System.EventHandler(this.menu_HelpAbout_Click);
//
// myPicture
//
this.myPicture.Location = new System.Drawing.Point(8, 16);
this.myPicture.Name = "myPicture";
this.myPicture.Size = new System.Drawing.Size(488, 184);
this.myPicture.TabIndex = 0;
this.myPicture.TabStop = false;
this.myPicture.Click = new System.EventHandler(this.myPicture_Click);
this.myPicture.Paint = new System.Windows.Forms.PaintEventHandler(this.myPicture_Paint);
this.myPicture.MouseUp = new System.Windows.Forms.MouseEventHandler(this.myPicture_MouseUp);
this.myPicture.MouseMove = new System.Windows.Forms.MouseEventHandler(this.myPicture_MouseMove);
this.myPicture.MouseDown = new System.Windows.Forms.MouseEventHandler(this.myPicture_MouseDown);
//
// MainToolBar
//
this.MainToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.tbb_Cursor,
this.tbb_Line,
this.tbb_Rect,
this.tbb_Circle});
this.MainToolBar.ButtonSize = new System.Drawing.Size(75, 22);
this.MainToolBar.DropDownArrows = true;
this.MainToolBar.Location = new System.Drawing.Point(0, 0);
this.MainToolBar.Name = "MainToolBar";
this.MainToolBar.ShowToolTips = true;
this.MainToolBar.Size = new System.Drawing.Size(520, 28);
this.MainToolBar.TabIndex = 1;
this.MainToolBar.TextAlign = System.Windows.Forms.ToolBarTextAlign.Right;
this.MainToolBar.ButtonClick = new System.Windows.Forms.ToolBarButtonClickEventHandler(this.MainToolBar_ButtonClick);
//
// tbb_Cursor
//
this.tbb_Cursor.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbb_Cursor.Text = "指针";
//
// tbb_Line
//
this.tbb_Line.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbb_Line.Text = "直线";
//
// tbb_Rect
//
this.tbb_Rect.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbb_Rect.Text = "矩形";
//
// tbb_Circle
//
this.tbb_Circle.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbb_Circle.Text = "圆形";
//
// myStatusBar
//
this.myStatusBar.Location = new System.Drawing.Point(0, 251);
this.myStatusBar.Name = "myStatusBar";
this.myStatusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.sbp_Info,
this.sbp_Coord});
this.myStatusBar.ShowPanels = true;
this.myStatusBar.Size = new System.Drawing.Size(520, 22);
this.myStatusBar.TabIndex = 2;
//
// sbp_Info
//
this.sbp_Info.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.sbp_Info.Width = 404;
//
// myPanel
//
this.myPanel.AutoScroll = true;
this.myPanel.Controls.Add(this.myPicture);
this.myPanel.Location = new System.Drawing.Point(0, 32);
this.myPanel.Name = "myPanel";
this.myPanel.Size = new System.Drawing.Size(512, 216);
this.myPanel.TabIndex = 3;
//
// openFileDialog1
//
this.openFileDialog1.DefaultExt = "*.pat";
this.openFileDialog1.Filter = "Painter文件|*.pat|BMP文件|*.bmp|JPG文件|*.jpg|JPEG文件|*.jpeg|Gif文件|*.gif";
//
// saveFileDialog1
//
this.saveFileDialog1.DefaultExt = "*.pat";
this.saveFileDialog1.Filter = "Painter文件|*.pat|BMP文件|*.bmp|JPG文件|*.jpg|JPEG文件|*.jpeg|Gif文件|*.gif";
//
// menu_EditDelItem
//
this.menu_EditDelItem.Index = 6;
this.menu_EditDelItem.Text = "删除元素";
this.menu_EditDelItem.Click = new System.EventHandler(this.menu_EditDelItem_Click);
//
// PaintForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 273);
this.Controls.Add(this.myPanel);
this.Controls.Add(this.myStatusBar);
this.Controls.Add(this.MainToolBar);
this.Menu = this.mainMenu;
this.Name = "PaintForm";
this.Text = "绘图板";
this.SizeChanged = new System.EventHandler(this.PaintForm_SizeChanged);
this.Load = new System.EventHandler(this.PaintForm_Load);
this.Paint = new System.Windows.Forms.PaintEventHandler(this.PaintForm_Paint);
((System.ComponentModel.ISupportInitialize)(this.sbp_Info)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.sbp_Coord)).EndInit();
this.myPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#endregion
#region 窗体事件响应
private void PaintForm_Load(object sender, System.EventArgs e)
{
ResizeControls();
this.myPicture.BackColor = Color.White;
this.tbb_Cursor.Pushed = true;
this.myPicture.Width = imgWidth;
this.myPicture.Height = imgHeight;
this.IsSaved = false;
}
private void PaintForm_SizeChanged(object sender, System.EventArgs e)
{
ResizeControls();
}
private void MainToolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button.Pushed)
{
foreach (ToolBarButton btn in MainToolBar.Buttons)
{
if (btn.Pushed && !btn.Equals(e.Button))
{
btn.Pushed = false;
}
}
}
if (e.Button.Equals(tbb_Cursor))
{
this.myPicture.Cursor = Cursors.Arrow;
}
else
{
this.myPicture.Cursor = Cursors.Cross;
}
}
private void PaintForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
PaintItems();
}
private void myPicture_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
PaintItems();
}
#endregion
#region 鼠标事件响应
private void myPicture_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (this.tbb_Cursor.Pushed)
{
Point pt = myPicture.PointToClient(Cursor.Position);
movex_start = movex_old = movex_new = pt.X;
movey_start = movey_old = movey_new = pt.Y;
if (e.Button==MouseButtons.Right && CursorState == ClickState.Free)
{
CursorState = ClickState.MoveBusy;
}
else if (e.Button==MouseButtons.Left && CursorState==ClickState.MoveBusy)
{
CursorState = ClickState.RotateBusy;
}
}
}
private void myPicture_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point pt = Cursor.Position;
pt = this.myPicture.PointToClient(pt);
this.sbp_Coord.Text = string.Format("({0},{1})",pt.X,pt.Y);
// 如果需要移动图形,则开始绘制
if (this.tbb_Cursor.Pushed && CursorState == ClickState.MoveBusy) {
movex_old = movex_new;
movey_old = movey_new;
movex_new = pt.X;
movey_new = pt.Y;
Graphics g = myPicture.CreateGraphics();
// 移动选中的Item
foreach (object obj in Items)
{
MyItem item = obj as MyItem;
if (item != null && item.IsSelected)
{
item.Move(g,movex_new-movex_old,movey_new-movey_old,
myPicture.BackColor);
}
}
PaintItems();
return;
}
//如果需要旋转图形,则开始绘制
if (this.tbb_Cursor.Pushed && CursorState == ClickState.RotateBusy)
{
movex_old = movex_new;
movey_old = movey_new;
movex_new = pt.X;
movey_new = pt.Y;
Graphics g = myPicture.CreateGraphics();
// 移动选中的Item
foreach (object obj in Items)
{
MyItem item = obj as MyItem;
if (item != null && item.IsSelected)
{
item.Rotate(g,movex_new,movey_new,myPicture.BackColor);
}
}
PaintItems();
return;
}
// 如果已经开始图形的绘制,则绘制图形
if (CursorState == ClickState.DrawBusy)
{
ptx_old = ptx_new;
pty_old = pty_new;
ptx_new = pt.X;
pty_new = pt.Y;
if (this.tbb_Line.Pushed)
{
Pen pen = new Pen(myPicture.BackColor,penwidth);
pen.DashStyle = penstyle;
Graphics g = myPicture.CreateGraphics();
g.DrawLine(pen,ptx_start,pty_start,ptx_old,pty_old);
pen.Color = pencolor;
g.DrawLine(pen,ptx_start,pty_start,ptx_new,pty_new);
}
else if (this.tbb_Rect.Pushed)
{
Pen pen = new Pen(myPicture.BackColor,penwidth);
pen.DashStyle = penstyle;
Graphics g = myPicture.CreateGraphics();
g.DrawRectangle(pen,ptx_start,pty_start,
ptx_old-ptx_start,pty_old-pty_start);
pen.Color = pencolor;
g.DrawRectangle(pen,ptx_start,pty_start,
ptx_new-ptx_start,pty_new-pty_start);
}
else if (this.tbb_Circle.Pushed)
{
Pen pen = new Pen(myPicture.BackColor,penwidth);
pen.DashStyle = penstyle;
Graphics g = myPicture.CreateGraphics();
g.DrawEllipse(pen,ptx_start,pty_start,
ptx_old-ptx_start,ptx_old-ptx_start);
pen.Color = pencolor;
g.DrawEllipse(pen,ptx_start,pty_start,
ptx_new-ptx_start,ptx_new-ptx_start);
}
PaintItems();
}
}
private void myPicture_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right) {
CursorState = ClickState.Free;
}
}
private void myPicture_Click(object sender, System.EventArgs e)
{
if (this.tbb_Cursor.Pushed)
{
if (CursorState == ClickState.Free ||
CursorState == ClickState.MoveBusy)
{
Point pt = this.myPicture.PointToClient(Cursor.Position);
bool HasSelected = false;
// 点击选择一个Item
foreach(object obj in this.Items) {
MyItem item = obj as MyItem;
if (item != null) {
if (item.Contains(pt.X,pt.Y)) {
item.Select(myPicture.CreateGraphics(),
myPicture.BackColor);
HasSelected = true;
break;
}
}
}
if (!HasSelected) {
foreach (object obj in this.Items) {
MyItem item = obj as MyItem;
if (item != null) {
item.DeSelect(myPicture.CreateGraphics(),
myPicture.BackColor);
}
}
}
}
}
else if (this.tbb_Line.Pushed)
{
if (CursorState == ClickState.Free)
{
CursorState = ClickState.DrawBusy;
this.myPicture.Capture = true;
Point pt = myPicture.PointToClient(Cursor.Position);
ptx_start = ptx_old = ptx_new = pt.X;
pty_start = pty_old = pty_new = pt.Y;
}
else
{
CursorState = ClickState.Free;
this.myPicture.Capture = false;
Point pt = myPicture.PointToClient(Cursor.Position);
ptx_new = pt.X;
pty_new = pt.Y;
MyItem item = ItemFactory.CreateItem(ItemType.Line,
ptx_start,pty_start,ptx_new,pty_new,pencolor,penwidth,penstyle);
Items.Add(item);
}
}
else if (this.tbb_Rect.Pushed)
{
if (CursorState == ClickState.Free)
{
CursorState = ClickState.DrawBusy;
this.myPicture.Capture = true;
Point pt = myPicture.PointToClient(Cursor.Position);
ptx_start = ptx_old = ptx_new = pt.X;
pty_start = pty_old = pty_new = pt.Y;
}
else
{
CursorState = ClickState.Free;
this.myPicture.Capture = false;
Point pt = myPicture.PointToClient(Cursor.Position);
ptx_new = pt.X;
pty_new = pt.Y;
MyItem item = ItemFactory.CreateItem(ItemType.RectAngle,
ptx_start,pty_start,ptx_new,pty_new,pencolor,penwidth,penstyle);
Items.Add(item);
}
}
else if (this.tbb_Circle.Pushed)
{
if (CursorState == ClickState.Free)
{
CursorState = ClickState.DrawBusy;
this.myPicture.Capture = true;
Point pt = myPicture.PointToClient(Cursor.Position);
ptx_start = ptx_old = ptx_new = pt.X;
pty_start = pty_old = pty_new = pt.Y;
}
else
{
CursorState = ClickState.Free;
this.myPicture.Capture = false;
Point pt = myPicture.PointToClient(Cursor.Position);
ptx_new = pt.X;
pty_new = pt.Y;
MyItem item = ItemFactory.CreateItem(ItemType.Circle,
ptx_start,pty_start,
ptx_new,pty_start ptx_new-ptx_start,
pencolor,penwidth,penstyle);
Items.Add(item);
}
}
}
#endregion
#region 菜单事件响应
private void menu_FileNew_Click(object sender, System.EventArgs e)
{
NewPaint pt = new NewPaint();
pt.ShowDialog(this);
this.myPicture.Width = pt.ImageWidth;
this.myPicture.Height = pt.ImageHeight;
imgWidth = pt.ImageWidth;
imgHeight = pt.ImageHeight;
this.Items.Clear();
this.myPicture.Invalidate();
this.IsSaved = false;
}
private void menu_FileOpen_Click(object sender, System.EventArgs e)
{
if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
FileName = openFileDialog1.FileName;
FileInfo finfo = new FileInfo(FileName);
if (finfo.Extension == ".pat")
{
System.Runtime.Serialization.IFormatter format =
new System.Runtime.Serialization.
Formatters.Binary.BinaryFormatter();
FileStream fs = finfo.Open(FileMode.OpenOrCreate,
FileAccess.ReadWrite,FileShare.ReadWrite);
this.Items.Clear();
Items = format.Deserialize(fs) as ArrayList;
fs.Close();
PaintItems();
}
else
{
ImgFileName = openFileDialog1.FileName;
Image img = Image.FromFile(ImgFileName);
this.myPicture.Width = img.Width;
this.myPicture.Height = img.Height;
this.myPicture.CreateGraphics().DrawImageUnscaled(
img,0,0);
}
}
}
private void menu_FileSave_Click(object sender, System.EventArgs e)
{
if (!IsSaved)
{
if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
SaveFile();
}
}
else
{
}
}
private void menu_FileSaveAs_Click(object sender, System.EventArgs e)
{
if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
SaveFile();
}
}
private void menu_EditClear_Click(object sender, System.EventArgs e)
{
this.Items.Clear();
this.myPicture.Invalidate();
}
private void menu_FileExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menu_EditSelAll_Click(object sender, System.EventArgs e)
{
foreach (object obj in Items) {
MyItem item = obj as MyItem;
if (item != null) {
item.Select(myPicture.CreateGraphics(),
myPicture.BackColor);
}
}
}
private void menu_EditSize_Click(object sender, System.EventArgs e)
{
NewPaint pt = new NewPaint(this.myPicture.Width,this.myPicture.Height);
pt.ShowDialog(this);
this.myPicture.Width = pt.ImageWidth;
this.myPicture.Height = pt.ImageHeight;
imgWidth = pt.ImageWidth;
imgHeight = pt.ImageHeight;
if (this.ImgFileName != null && ImgFileName != "") {
Image img = Image.FromFile(ImgFileName);
this.myPicture.CreateGraphics().DrawImage(
img,0,0,imgWidth,imgHeight);
}
PaintItems();
}
private void menu_EditDelItem_Click(object sender, System.EventArgs e)
{
foreach (object obj in Items)
{
MyItem item = obj as MyItem;
if (item != null && item.IsSelected)
{
Items.Remove(item);
}
}
this.myPicture.Invalidate();
}
private void menu_HelpAbout_Click(object sender, System.EventArgs e)
{
MessageBox.Show("由无名工作室出品!","绘图板");
}
private void menu_PenColor_Click(object sender, System.EventArgs e)
{
if (this.colorDialog1.ShowDialog(this) == DialogResult.OK) {
this.pencolor = colorDialog1.Color;
}
}
private void menu_PenWidth_Click(object sender, System.EventArgs e)
{
ChooseWidth cw = new ChooseWidth();
cw.ShowDialog(this);
this.penwidth = cw.PenWidth;
}
private void menu_PenStyle_Click(object sender, System.EventArgs e)
{
ChooseStyle cs = new ChooseStyle();
cs.ShowDialog(this);
this.penstyle = cs.PenStyle;
}
#endregion
#region 自定义方法和字段
private void ResizeControls()
{
this.myPanel.Left = this.ClientRectangle.Left;
this.myPanel.Top = this.ClientRectangle.Top this.MainToolBar.Height;
this.myPanel.Width = this.ClientRectangle.Width;
this.myPanel.Height = this.ClientRectangle.Height
- this.MainToolBar.Height - this.myStatusBar.Height;
this.myPicture.Left = 0;
this.myPicture.Top = 0;
}
private void PaintItems()
{
Graphics g = myPicture.CreateGraphics();
if (ImgFileName != null && ImgFileName!="")
{
Image img = Image.FromFile(ImgFileName);
this.myPicture.Width = imgWidth;
this.myPicture.Height = imgHeight;
g.DrawImage(img,0,0,imgWidth,imgHeight);
}
foreach (object obj in Items)
{
MyItem Item = obj as MyItem;
if (Item != null)
{
Item.Draw(g,myPicture.BackColor);
}
}
}
private void SaveFile()
{
FileName = saveFileDialog1.FileName;
FileInfo finfo = new FileInfo(FileName);
IsSaved = true;
if (finfo.Extension == ".pat") // 保存绘图板文件
{
System.Runtime.Serialization.IFormatter format =
new System.Runtime.Serialization.
Formatters.Binary.BinaryFormatter();
FileStream fs = finfo.Open(FileMode.OpenOrCreate,
FileAccess.ReadWrite,FileShare.ReadWrite);
format.Serialize(fs,Items);
fs.Close();
}
else // 保存图像文件
{
System.Drawing.Bitmap bmp =
new Bitmap(myPicture.Width,myPicture.Height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
foreach (object obj in Items)
{
MyItem item = obj as MyItem;
if (item != null)
{
item.Draw(g,myPicture.BackColor);
}
}
System.Drawing.Imaging.ImageFormat format = null;
switch(finfo.Extension)
{
case ".bmp":
format = ImageFormat.Bmp;
break;
case ".jpg":
case ".jpeg":
format = ImageFormat.Jpeg;
break;
case ".gif":
format = ImageFormat.Gif;
break;
}
bmp.Save(FileName,format);
}
}
#endregion
}
public enum ClickState{Free,DrawBusy,MoveBusy,RotateBusy}
}
标签: 绘图
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论