[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication6
{
public partial class Form1 : Form
{
private Bitmap smallBmp;
private Bitmap bmp; //holds original loaded image
private Bitmap newbmp; //holds latest version of image
private bool imageStatus = false; //indicates image is loaded
private int resizeLevel; //level image magnified/reduced
private Point lastPoint = Point.Empty;//tracks mouse movement
private Point origPoint = Point.Empty;//mouse down coordinates
private Rectangle rectSel;//select area
private bool selectStatus;//true if area selected
public Form1()
{
InitializeComponent();
panel1.MouseDown+=new MouseEventHandler(panel1_MouseDown);
panel1.MouseUp+=new MouseEventHandler(panel1_MouseUp);
panel1.MouseMove+=new MouseEventHandler(panel1_MouseMove);
panel1.Paint+=new PaintEventHandler(panel1_Paint);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openMenuItem_Click(object sender, EventArgs e)
{
//load image from file
OpenFileDialog fd = new OpenFileDialog();
fd.InitialDirectory = @"D:\My Documents\My Pictures";
fd.Filter = "Image File|*.jpg;*.gif";
if (fd.ShowDialog() == DialogResult.OK)
{
string fname = fd.FileName;
using (Graphics g = panel1.CreateGraphics())
{
bmp = new Bitmap(fname);
newbmp = bmp;
g.FillRectangle(Brushes.White, 0, 0, panel1.Width, panel1.Height);
Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
g.DrawImage(bmp, r);
imageStatus = true;
Graphics gClear = panel2.CreateGraphics();
gClear.FillRectangle(Brushes.White, 0, 0, panel2.Width, panel2.Height);
gClear.Dispose();
}
}
}
private void mirrorMenuItem_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
int h = newbmp.Height;
int w = newbmp.Width;
Point[] destPts ={
new Point(w,0),
new Point(0,0),
new Point(w,h)
};
Bitmap tempBmp = new Bitmap(w, h);
Graphics gr = Graphics.FromImage(tempBmp);
gr.DrawImage(newbmp, destPts);
g.DrawImage(tempBmp, 0, 0);
newbmp = tempBmp;
g.Dispose();
gr.Dispose();
}
private void 翻转VToolStripMenuItem_Click(object sender, EventArgs e)
{
newbmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
Graphics g = panel1.CreateGraphics();
g.DrawImage(newbmp, 0, 0);
g.Dispose();
}
private void 刷屏RToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Invalidate();
panel1.Update();
selectStatus = false;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//redraw part of current image to panel
if (imageStatus)
{
g.DrawImage(newbmp, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
}
base.OnPaint(e);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (lastPoint != Point.Empty)
{
panel1.Invalidate(rectSel);
panel1.Update();
}
lastPoint.X = e.X;
lastPoint.Y = e.Y;
origPoint = lastPoint;
selectStatus = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
rectSel.X = e.X;
if (e.X > origPoint.X)
{
rectSel.X = origPoint.X;
}
rectSel.Y = origPoint.Y;
rectSel.Width = Math.Abs(e.X - origPoint.X) + 1;
rectSel.Height = Math.Abs(e.Y - origPoint.Y) + 1;
origPoint = Point.Empty;
if (rectSel.Width < 2)
{
selectStatus = false;
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (origPoint != Point.Empty)
{
Rectangle r;
Rectangle rd;
int xop = origPoint.X;
if (xop > lastPoint.X)
{
xop = lastPoint.X;
}
int w = Math.Abs(origPoint.X - lastPoint.X) + 1;
int h = lastPoint.Y - origPoint.Y + 1;
r = new Rectangle(xop, origPoint.Y, w, h);
xop = e.X >= origPoint.X ? origPoint.X : e.X;
w = Math.Abs(origPoint.X - e.X);
h = e.Y - origPoint.Y;
rd = new Rectangle(xop, origPoint.Y, w, h);
Graphics g = panel1.CreateGraphics();
g.DrawImage(newbmp,r);
//newbmp.MakeTransparent();
g.DrawRectangle(Pens.Red, rd);
g.Dispose();
lastPoint.X = e.X;
lastPoint.Y = e.Y;
}
}
private void 查看cToolStripMenuItem_Click(object sender, EventArgs e)
{
if (selectStatus)
{
Graphics g = panel2.CreateGraphics();
g.FillRectangle(Brushes.White, panel2.ClientRectangle);
Rectangle rd = new Rectangle(0, 0, rectSel.Width, rectSel.Height);
Bitmap temp = new Bitmap(rectSel.Width, rectSel.Height);
Graphics gi = Graphics.FromImage(temp);
gi.DrawImage(newbmp, rd, rectSel, GraphicsUnit.Pixel);
smallBmp = temp;
g.DrawImage(smallBmp, rd);
g.Dispose();
resizeLevel = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
Graphics g = panel2.CreateGraphics();
if (smallBmp != null)
{
resizeLevel = resizeLevel + 1;
float fac = (float)(1.0 + (resizeLevel * .25));
int w = (int)(smallBmp.Width * fac);
int h = (int)(smallBmp.Height * fac);
Rectangle rd = new Rectangle(0, 0, w, h);
Bitmap tempBmp = new Bitmap(w, h);
Graphics gi = Graphics.FromImage(tempBmp);
gi.DrawImage(smallBmp, rd);
g.DrawImage(tempBmp,rd);
gi.Dispose();
}
g.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
Graphics g = panel2.CreateGraphics();
if (smallBmp != null)
{
resizeLevel = (resizeLevel > -3) ? resizeLevel - 1 : resizeLevel;
float fac = (float)(1.0 + (resizeLevel * .25));
int w = (int)(smallBmp.Width * fac);
int h = (int)(smallBmp.Height * fac);
Rectangle rd = new Rectangle(0, 0, w, h);
Bitmap tempBmp = new Bitmap(w, h);
Graphics gi = Graphics.FromImage(tempBmp);
g.FillRectangle(Brushes.White, panel2.ClientRectangle);
gi.DrawImage(smallBmp, rd);
g.DrawImage(tempBmp, rd);
gi.Dispose();
//panel2.Invalidate();
//panel2.Update();
}
g.Dispose();
}
}
}
Form1.Designer.cs页面
namespace WindowsApplication6
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.文件FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.图像IToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.mirrorMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.屏幕SToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.panel1 = new System.Windows.Forms.Panel();
this.panel2 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.翻转VToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.刷屏RToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.查看cToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.文件FToolStripMenuItem,
this.图像IToolStripMenuItem,
this.屏幕SToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(655, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// 文件FToolStripMenuItem
//
this.文件FToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.openMenuItem});
this.文件FToolStripMenuItem.Name = "文件FToolStripMenuItem";
this.文件FToolStripMenuItem.Size = new System.Drawing.Size(73, 20);
this.文件FToolStripMenuItem.Text = "文件(&F)";
//
// openMenuItem
//
this.openMenuItem.Name = "openMenuItem";
this.openMenuItem.Size = new System.Drawing.Size(114, 22);
this.openMenuItem.Text = "打开(&O)";
this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);
//
// 图像IToolStripMenuItem
//
this.图像IToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.mirrorMenuItem,
this.翻转VToolStripMenuItem,
this.查看cToolStripMenuItem});
this.图像IToolStripMenuItem.Name = "图像IToolStripMenuItem";
this.图像IToolStripMenuItem.Size = new System.Drawing.Size(71, 20);
this.图像IToolStripMenuItem.Text = "图像(&I)";
//
// mirrorMenuItem
//
this.mirrorMenuItem.Name = "mirrorMenuItem";
this.mirrorMenuItem.Size = new System.Drawing.Size(152, 22);
this.mirrorMenuItem.Text = "镜像(&M)";
this.mirrorMenuItem.Click += new System.EventHandler(this.mirrorMenuItem_Click);
//
// 屏幕SToolStripMenuItem
//
this.屏幕SToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.刷屏RToolStripMenuItem});
this.屏幕SToolStripMenuItem.Name = "屏幕SToolStripMenuItem";
this.屏幕SToolStripMenuItem.Size = new System.Drawing.Size(73, 20);
this.屏幕SToolStripMenuItem.Text = "屏幕(&S)";
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 27);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(327, 327);
this.panel1.TabIndex = 1;
//
// panel2
//
this.panel2.Location = new System.Drawing.Point(364, 27);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(273, 262);
this.panel2.TabIndex = 2;
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(437, 315);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(39, 23);
this.button1.TabIndex = 3;
this.button1.Text = "+";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button2.Location = new System.Drawing.Point(539, 315);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(39, 23);
this.button2.TabIndex = 4;
this.button2.Text = "-";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// 翻转VToolStripMenuItem
//
this.翻转VToolStripMenuItem.Name = "翻转VToolStripMenuItem";
this.翻转VToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.翻转VToolStripMenuItem.Text = "翻转(&V)";
this.翻转VToolStripMenuItem.Click += new System.EventHandler(this.翻转VToolStripMenuItem_Click);
//
// 刷屏RToolStripMenuItem
//
this.刷屏RToolStripMenuItem.Name = "刷屏RToolStripMenuItem";
this.刷屏RToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.刷屏RToolStripMenuItem.Text = "刷屏(&R)";
this.刷屏RToolStripMenuItem.Click += new System.EventHandler(this.刷屏RToolStripMenuItem_Click);
//
// 查看cToolStripMenuItem
//
this.查看cToolStripMenuItem.Name = "查看cToolStripMenuItem";
this.查看cToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.查看cToolStripMenuItem.Text = "查看(&c)";
this.查看cToolStripMenuItem.Click += new System.EventHandler(this.查看cToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(655, 375);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.menuStrip1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 文件FToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 图像IToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 屏幕SToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem openMenuItem;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ToolStripMenuItem mirrorMenuItem;
private System.Windows.Forms.ToolStripMenuItem 翻转VToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 刷屏RToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 查看cToolStripMenuItem;
}
}
Progrem.cs页面
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication6
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}