基本信息
源码名称:画线的新想法
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2015-09-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
/*
* *******************************
* Line Editor by Marco Bellini *
* *
* Please send me feedbacks! :D *
* butch.er@tin.it *
* *
* ...and share Knowledge! *
* *
* *******************************
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace LineEditor
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.PictureBox image;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem15;
private System.Windows.Forms.SaveFileDialog sfd;
private System.Windows.Forms.MenuItem menuItem17;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog ofd;
private System.Windows.Forms.ContextMenu linecmenu;
private System.Windows.Forms.MenuItem LWidth;
private System.Windows.Forms.MenuItem Delete;
Bitmap bmpBack = null;
private bool isSelected = false;
private int _X, _Y;
private ArrayList Lines = null;
private struct Line
{
public MarkControl mark1;
public MarkControl mark2;
public int Width;
}
public Form1()
{
InitializeComponent();
Lines = new ArrayList();
bmpBack = new Bitmap(image.Width,image.Height);
Graphics.FromImage(bmpBack).Clear(Color.White);
image.Image = (Bitmap)bmpBack.Clone();
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.image = new System.Windows.Forms.PictureBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem17 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.ofd = new System.Windows.Forms.OpenFileDialog();
this.sfd = new System.Windows.Forms.SaveFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.linecmenu = new System.Windows.Forms.ContextMenu();
this.LWidth = new System.Windows.Forms.MenuItem();
this.Delete = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// image
//
this.image.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.image.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.image.Location = new System.Drawing.Point(16, 16);
this.image.Name = "image";
this.image.Size = new System.Drawing.Size(400, 400);
this.image.TabIndex = 3;
this.image.TabStop = false;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem17,
this.menuItem2,
this.menuItem15,
this.menuItem4,
this.menuItem3});
this.menuItem1.Text = "&File";
//
// menuItem17
//
this.menuItem17.Index = 0;
this.menuItem17.Text = "&New";
this.menuItem17.Click = new System.EventHandler(this.menuItem17_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "&Load background";
this.menuItem2.Click = new System.EventHandler(this.menuItem2_Click);
//
// menuItem15
//
this.menuItem15.Index = 2;
this.menuItem15.Text = "&Save";
this.menuItem15.Click = new System.EventHandler(this.menuItem15_Click);
//
// menuItem4
//
this.menuItem4.Index = 3;
this.menuItem4.Text = "-";
//
// menuItem3
//
this.menuItem3.Index = 4;
this.menuItem3.Text = "&Exit";
this.menuItem3.Click = new System.EventHandler(this.menuItem3_Click);
//
// ofd
//
this.ofd.Filter = "Image Files|*.*";
this.ofd.FileOk = new System.ComponentModel.CancelEventHandler(this.ofd_FileOk);
//
// sfd
//
this.sfd.DefaultExt = "jpg";
this.sfd.Filter = "JPEG Image Files (*.jpg)|*.jpg";
this.sfd.Title = "Save Cover Page";
this.sfd.FileOk = new System.ComponentModel.CancelEventHandler(this.sfd_FileOk);
//
// button1
//
this.button1.Location = new System.Drawing.Point(424, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 40);
this.button1.TabIndex = 11;
this.button1.Text = "Draw Line";
this.button1.Click = new System.EventHandler(this.button1_Click);
//
// linecmenu
//
this.linecmenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.LWidth,
this.Delete});
//
// LWidth
//
this.LWidth.Index = 0;
this.LWidth.Text = "Width";
this.LWidth.Click = new System.EventHandler(this.Width_Click);
//
// Delete
//
this.Delete.Index = 1;
this.Delete.Text = "Delete";
this.Delete.Click = new System.EventHandler(this.Delete_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.AutoScroll = true;
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(552, 453);
this.Controls.Add(this.button1);
this.Controls.Add(this.image);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Line Editor";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
ofd.AddExtension = true;
ofd.CheckFileExists = true;
ofd.Title = "Select an image file";
ofd.ReadOnlyChecked = true;
ofd.InitialDirectory = Application.StartupPath;
ofd.Multiselect = false;
ofd.Filter = "JPEG Files (*.jpg)|*.jpg|Image Files(*.*)|*.*";
ofd.ShowDialog();
}
//Loads bg
private void ofd_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
bmpBack = new Bitmap(ofd.FileName);
image.Image = (Bitmap)bmpBack.Clone();
image.SendToBack();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Redraw();
}
private void menuItem15_Click(object sender, System.EventArgs e)
{
sfd.InitialDirectory = Application.StartupPath;
sfd.ShowDialog();
}
//Saves file
private void sfd_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
image.Image.Save(sfd.FileName,ImageFormat.Jpeg);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
//Exit
private void menuItem3_Click(object sender, System.EventArgs e)
{
this.Close();
}
//New
private void menuItem17_Click(object sender, System.EventArgs e)
{
image.Controls.Clear();
bmpBack = new Bitmap(image.Width,image.Height);
image.Image = (Bitmap)bmpBack.Clone();
Lines.Clear();
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
//Adds red marks that are the beginning/end of the line
MarkControl mark1 = new MarkControl();
mark1.Location = new Point(50,50);
mark1.ContextMenu = linecmenu;
image.Controls.Add(mark1);
MarkControl mark2 = new MarkControl();
mark2.Location = new Point(100,100);
mark2.ContextMenu = linecmenu;
image.Controls.Add(mark2);
//Line Struct contains the information for a single line
Line line = new Line();
line.mark1 = mark1;
line.mark2 = mark2;
line.Width = 1;
//Events for moving marks
mark1.MouseUp = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseUp);
mark1.MouseDown = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseDown);
mark1.MouseMove = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseMove);
mark2.MouseUp = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseUp);
mark2.MouseDown = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseDown);
mark2.MouseMove = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseMove);
//Adds Line object to an arraylist
Lines.Add(line);
Redraw();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Simply draws a line
private void DrawLine(Line line)
{
Graphics g = null;
g = Graphics.FromImage(image.Image);
g.DrawLine(new Pen(Color.Black,(float)line.Width),line.mark1.Center.X,line.mark1.Center.Y,line.mark2.Center.X,line.mark2.Center.Y);
g.Dispose();
}
//Redraws all the lines and a part of the background
private void Redraw(Line line, Point p)
{
Graphics.FromImage(image.Image).DrawImage(bmpBack,0,0,image.Image.Width,
image.Image.Height);
foreach(Line l in Lines)
{
DrawLine(l);
}
Region r = getRegionByLine(line,p);
image.Invalidate(r);
image.Update();
}
//Redraws all the lines and the background too
private void Redraw()
{
if(bmpBack!=null)
image.Image = (Bitmap)bmpBack.Clone();
else
{
image.Image = new Bitmap(image.Width,image.Height);
Graphics.FromImage(image.Image).Clear(Color.Transparent);
}
foreach(Line l in Lines)
{
DrawLine(l);
}
image.Refresh();
}
private void Mark_MouseDown(object sender, MouseEventArgs e)
{
this.SuspendLayout();
isSelected = true;
_X = e.X;
_Y = e.Y;
}
private void Mark_MouseMove(object sender, MouseEventArgs e)
{
if(isSelected)
{
MarkControl mc1 = (MarkControl)sender;
Line l = getLineByMark(mc1);
Point p = new Point(e.X - _X mc1.Left, e.Y - _Y mc1.Top);
mc1.Location = p;
Redraw(l,p);
}
}
private void Mark_MouseUp(object sender, MouseEventArgs e)
{
isSelected = false;
ResumeLayout();
Redraw();
}
//Retrieves a mark having the other one
private MarkControl getOtherMark(MarkControl m)
{
foreach(Line l in Lines)
{
if(l.mark1 == m)
return l.mark2;
if(l.mark2 == m)
return l.mark1;
}//Never happens :D
throw new Exception("No relative mark found");
}
//Retrieves a Line object having a mark
private Line getLineByMark(MarkControl m)
{
foreach(Line l in Lines)
{
if(l.mark1 == m || l.mark2 == m)
return l;
}//Never happens :D
throw new Exception("No line found");
}
//Shows Dialog to change line's width
private void Width_Click(object sender, System.EventArgs e)
{
Line l;
wdialog wd = new wdialog(getLineByMark((MarkControl)linecmenu.SourceControl).Width);
DialogResult res = wd.ShowDialog();
if(res == DialogResult.OK)
{
l = getLineByMark((MarkControl)linecmenu.SourceControl);
Lines.Remove(l);
l.Width = wd.LineWidth;
Lines.Add(l);
Redraw();
}
}
//Deletes line
private void Delete_Click(object sender, System.EventArgs e)
{
Line l = getLineByMark(((MarkControl)linecmenu.SourceControl));
Lines.Remove(l);
l.mark1.Dispose();
l.mark2.Dispose();
linecmenu.SourceControl.Dispose();
Redraw();
}
//Returns the region to update
private Region getRegionByLine(Line l, Point p)
{
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(new Point[]{l.mark1.Center,l.mark2.Center,p,l.mark1.Center});
RectangleF rf = gp.GetBounds();
gp.Dispose();
rf.Inflate(100f,100f);
return new Region(rf);
}
}
}