using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Rectangle rect = new Rectangle(0, 0, 0, 0);
Point p1 = new Point(0, 0);
ArrayList bitmaps = new ArrayList();
List<Image> pic = new List<Image>();
//PictureBox pix1 = new PictureBox();
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
for (int i = 0; i < openFileDialog1.FileNames.Length; ++i)
{
listBox1.Items.Add(openFileDialog1.FileNames[i]);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// --- select list item
int selectIndex = listBox1.SelectedIndex;
int tlong = listBox1.Items.Count;
if (selectIndex < 0 || selectIndex > tlong)
{
return;
}
String file = (String)listBox1.Items[selectIndex];
System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
System.IO.FileStream fileStream = fileInfo.OpenRead();
pictureBox1.Image = System.Drawing.Image.FromStream(fileStream);
}
private void button2_Click(object sender, EventArgs e)
{
while (listBox1.SelectedItems.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItems[0]);
pictureBox1.Image = null;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
int curValue = trackBar1.Value;
Console.WriteLine((int)(curValue * 512.0 / 100));
pictureBox1.Width = (int)(curValue * 512.0 / 100);
pictureBox1.Height = (int)(curValue * 512.0 / 100);
}
//在picturebox1中截图
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
rect.X = e.X;
rect.Y = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X < rect.X)
p1.X = e.X;
else
p1.X = rect.X;
if (e.Y < rect.Y)
p1.Y = e.Y;
else
p1.Y = rect.Y;
pictureBox3.Left = pictureBox1.Left + p1.X;
pictureBox3.Top = pictureBox1.Top + p1.Y;
pictureBox3.Width = Math.Abs(e.X - rect.X);
pictureBox3.Height = Math.Abs(e.Y - rect.Y);
pictureBox3.Visible = true;
}
else
pictureBox3.Visible = false;
}
private int count = 0;
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
try
{
rect.X = p1.X;
rect.Y = p1.Y;
rect.Width = pictureBox3.Width;
rect.Height = pictureBox3.Height;
bmp = new Bitmap(pictureBox1.Image);
Bitmap newBitmap = bmp.Clone(rect, bmp.PixelFormat);
//pictureBox2.Image = newBitmap;
bitmaps.Add(newBitmap);
pic.Add(newBitmap);
this.imageList1.Images.Add(newBitmap);
this.listView1.Items.Add(count.ToString(), count);
count++;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine(ex.ToString());
}
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
{
saveFileDialog1.Title = "另存为";
saveFileDialog1.InitialDirectory = @"C:\";
saveFileDialog1.Filter = "图片(*.png)|*.png|图片(*.jpg)|*.jpg|图片(*.gif)|*.gif";
saveFileDialog1.AddExtension = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Rectangle rc = new Rectangle(0, 0, panel1.Width, panel1.Height);
Bitmap bmp = new Bitmap(rc.Width, rc.Height);
panel1.DrawToBitmap(bmp, rc);
bmp.Save(saveFileDialog1.FileName);
//Graphics g = Graphics.FromImage(panel1.);
//using (Image img = pictureBox2.Image)
//{
// img.Save(saveFileDialog1.FileName);
//}
}
}
}
/*
protected override void OnMouseDown(MouseEventArgs e)
{
startPoint = this.PointToScreen(e.Location);
lastRect = new Rectangle(0, 0, 0, 0);
isDragging = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (isDragging)
{
Point current = this.PointToScreen(e.Location);
int width = current.X - startPoint.X;
int height = current.Y - startPoint.Y;
ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);
lastRect = new Rectangle(startPoint, new Size(width, height));
ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (isDragging)
{
isDragging = false;
ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);
MessageBox.Show("the rect is " + lastRect);
}
}
*/
private void button4_Click(object sender, EventArgs e)
{
string filename = "QSMC_Menu";//文件名称
string path = "c:\\" + filename.ToString() + ".txt";//文件路径,假设放在了C盘根目录。
if (CreatFile(path))
{
//向txt文件输入标题信息
TxtExport(path, "TYPE name id x y w h u v uw vh zdepth font_name font_size textID ");
//向txt文件输入坐标等信息
string type = fun("bar");
string name = fun("bg0");
string id = fun("0");
string x = fun(p1.X.ToString());
string y = fun(p1.Y.ToString());
string w = fun(pictureBox3.Width.ToString());
string h = fun(pictureBox3.Height.ToString());
string u = fun(p.X.ToString());
string v = fun(p.Y.ToString());
string uw = fun(pictureBox3.Width.ToString());
string vh = fun(pictureBox3.Height.ToString());
string zdepth = fun(" ");
string font_name = fun(" ");
string font_size = fun(" ");
string textID = fun(" ");
string temp = type + "\t" + name + "\t" + id + "\t" + x + "\t" + y + "\t" + w + "\t" + h + "\t" + u + "\t" + v + "\t" + uw + "\t" + vh + "\t" + zdepth + "\t" + font_name + "\t" + font_size + "\t" + textID; //写入txt文件
TxtExport(path, temp);
}
}
//控制字符串长度为8
public string fun(string str)
{
while (str.Length < 8)
str += " ";
return str;
}
#region 导出数据
/// <summary>
/// 导出数据
/// </summary>
/// <param name="FullFileName"></param>
/// <param name="TextAll"></param>
/// <returns></returns>
private bool TxtExport(string FullFileName, string TextAll)
{
if (!CreatFile(FullFileName))
{
return false;
}
StreamWriter sw = new StreamWriter(FullFileName, true, Encoding.Default); //该编码类型不会改变已有文件的编码类型
sw.WriteLine(TextAll);
sw.Close();
return true;
}
#endregion
#region 创建文件(文件存在则跳过)
/// <summary>
/// 创建文件(文件存在则跳过)
/// </summary>
/// <param name="FullFileName">带路径的文件名</param>
/// <returns></returns>
private bool CreatFile(string FullFileName)
{
if (File.Exists(FullFileName))
{
return true;
}
else
{
try
{
FileStream fs = new FileStream(FullFileName, FileMode.CreateNew);
fs.Close();
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
return false;
}
}
}
#endregion
private void pictureBox2_Click(object sender, EventArgs e)
{
}
bool wselected = false;
Point p = new Point();
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
pictureBox2.Cursor = Cursors.Hand; //按下鼠标时,将鼠标形状改为手型
wselected = true;
p.X = e.X;
p.Y = e.Y;
}
int driftX = 0, driftY = 0;
int mx = 0, my = 0;
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (wselected)
{
driftX = p.X - e.X;
driftY = p.Y - e.Y;
mx = mx - driftX;
my = my - driftY;
Bitmap JPEG = new Bitmap(this.pictureBox2.Image);
Graphics g = pictureBox2.CreateGraphics();
g.Clear(pictureBox2.BackColor);
g.DrawImage(JPEG, mx, my);
p.X = e.X;
p.Y = e.Y;
JPEG.Dispose();
g.Dispose();//图像移动的距离
}
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
pictureBox2.Cursor = Cursors.Default; //松开鼠标时,形状恢复为箭头
wselected = false;
this.pictureBox2.Cursor = Cursors.Default;
}
private void button5_Click(object sender, EventArgs e)
{
string filename = "QSMC_Menu";//文件名称
string path = "c:\\" + filename.ToString() + ".txt";//文件路径,假设放在了C盘根目录。
if (CreatFile(path))
{
//向txt文件输入标题信息
TxtExport(path, "TYPE name id x y w h u v uw vh zdepth font_name font_size textID ");
//向txt文件输入坐标等信息
string type = fun("button");
string name = fun("bg0");
string id = fun("0");
string x = fun(p1.X.ToString());
string y = fun(p1.Y.ToString());
string w = fun(pictureBox3.Width.ToString());
string h = fun(pictureBox3.Height.ToString());
string u = fun(p.X.ToString());
string v = fun(p.Y.ToString());
string uw = fun(pictureBox3.Width.ToString());
string vh = fun(pictureBox3.Height.ToString());
string zdepth = fun(" ");
string font_name = fun(" ");
string font_size = fun(" ");
string textID = fun(" ");
string temp = type + "\t" + name + "\t" + id + "\t" + x + "\t" + y + "\t" + w + "\t" + h + "\t" + u + "\t" + v + "\t" + uw + "\t" + vh + "\t" + zdepth + "\t" + font_name + "\t" + font_size + "\t" + textID; //写入txt文件
TxtExport(path, temp);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
// pictureBox2.Image = imageList1.Images[listView1.FocusedItem.ImageIndex];
}
Point _oldPosition = new Point();
void pix1_MouseMove(object sender, MouseEventArgs e)
{
PictureBox p = sender as PictureBox;
if (p == null) return;
if (e.Button == MouseButtons.Left)
{
p.Left += Cursor.Position.X - _oldPosition.X;
p.Top += Cursor.Position.Y - _oldPosition.Y;
_oldPosition = Cursor.Position;
}
}
void pix1_MouseDown(object sender, MouseEventArgs e)
{
_oldPosition = Cursor.Position;
}
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
}
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
PictureBox pix1 = new PictureBox();
//pix1.Image = imageList1.Images[listView1.FocusedItem.ImageIndex];
pix1.Image = pic[listView1.FocusedItem.ImageIndex];
pix1.SizeMode = PictureBoxSizeMode.AutoSize;
pix1.MouseDown += new MouseEventHandler(pix1_MouseDown);
pix1.MouseMove += new MouseEventHandler(pix1_MouseMove);
foreach (Control c in panel1.Controls)
{
if (c is PictureBox)
{
PictureBox temp_pic = ((PictureBox)c);
if (temp_pic.Image == pix1.Image)
return;
}
}
this.panel1.Controls.Add(pix1);
//foreach(PictureBox pix2 in panel1.Controls)
//{
// pix1.Enabled=false;//首先设置所有的PictureBox为未激活状态
//}
//panel1.Controls[listView1.SelectedItems].Enabled = true;//然后设置选择项对应的PictureBox为激活状态
}
}
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
listView1.FocusedItem.BeginEdit();
}
//private void listView1_MouseClick(object sender, MouseEventArgs e)
//{
// if (e.Button == MouseButtons.Right)
// {
// if (listView1.SelectedItems.Count != 0)
// {
// contextMenuStrip1.Show(MousePosition);
// }
// }
//}
//private void button6_Click(object sender, EventArgs e)
//{
// while (listView1.SelectedItems.Count > 0)
// {
// PictureBox pix = new PictureBox();
// pix.Image = pic[listView1.FocusedItem.ImageIndex];
// foreach (Control c in panel1.Controls)
// {
// if (c is PictureBox)
// {
// PictureBox temp_pic = ((PictureBox)c);
// if (temp_pic.Image == pix.Image)
// }
// }
//pic[listView1.FocusedItem.ImageIndex] = null;
//}
//if (this.listView1.SelectedItems != null && this.listView1.SelectedItems.Count > 0)
//{
// this.listView1.Items.Remove(this.listView1.SelectedItems[0]);
//}
//int selectcount = listView1.SelectedItems.Count;
//for (int i = 0; i < selectcount; i++)
//{
// listView1.SelectedItems[0].Remove();
//}
}
}
对picturebox进行多次截图,输出截图的合成图(未完成)
最新推荐文章于 2023-08-24 16:07:11 发布