您的位置:首页 > 编程语言 > C#

.net 学习笔记(一):C#版图片浏览器

2012-04-21 10:20 393 查看
因这学期选修了.net,而这门课每一个实验跟综合性实验没什么不同,都得做成一个应用程序。昨晚写完了这个实验的报告就顺便贴上来了。

首先上几个截图:

程序运行主窗口:





双击缩略图之后创建图片查看器:





1. 顺时针、逆时针:








实际大小(允许使用鼠标拖拽):









屏幕截图:

选定区域





双击区域弹出保存窗口:





缩略图核心代码:

private void ThreadProcSafe(object arg)
{
this.CreateMyListView((string[])arg);
}
private void CreateMyListView(string[] arg)
{
if (listView1 == null)
return;
if (this.listView1.InvokeRequired)
{
this.Invoke(new CreateMyListViewCallback(this.CreateMyListView), new object[] { arg });
}
else
{
string[] path = arg;
listView1.View = View.LargeIcon;
listView1.Items.Clear();
ImageList imageListLarge = new ImageList();
listView1.LargeImageList = imageListLarge;
imageListLarge.ImageSize = new System.Drawing.Size(120, 112);
this.Cursor = Cursors.WaitCursor;
for (int i = 0; i < path.Length; i++)
{
try
{
imageListLarge.Images.Add(System.Drawing.Image.FromFile(path[i]));
this.listView1.Items.Add(path[i], i);
}
catch { }
}
this.Cursor = Cursors.AppStarting;
}
}


拖拽功能实现核心代码:

private void moveRepaint(myPoint po)
{

Graphics g = this.pictureBox1.CreateGraphics();
int width = this.pictureBox1.Width;
int height = this.pictureBox1.Height;
Rectangle destPara = new Rectangle(pic.getActual().Width, pic.getActual().Height, width, height);
// Create rectangle for source image.
Rectangle srcRect = new Rectangle(po.x, po.y, width, height);
GraphicsUnit units = GraphicsUnit.Pixel;
g.DrawImage(this.pictureBox1 .Image, destPara, srcRect, units);
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Left&&!flag)
{
mouseDown = true;
mousePoint.x = e.X;
mousePoint.y = e.Y;

}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{

if (mouseDown)
{

sft.x = mousePoint.x - e.X;
sft.y = mousePoint.y - e.Y;
this.moveRepaint(pic.getChange(sft));
}

}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (!flag)
{
mouseDown = false;
sft.x = mousePoint.x - e.X;
sft.y = mousePoint.y - e.Y;
pic.setLocalPoin(pic.getChange(sft));
}
}


幻灯片功能实现核心代码:

public partial class PowerPoint : Form
{
private Bitmap myBitmap;
int index = 0;
string[] files;
public PowerPoint(string[] files)
{
this.files = files;
InitializeComponent();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.TopMost = true ;
this.timer1.Enabled = true;
}
private void timer2_Tick(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(showPicture);
Thread thread = new Thread(threadStart);
thread.Start();

}

public void showPicture()
{
if (this.index < files.Length)
{
myBitmap = new Bitmap(files[this.index]);
if (this.pictureBox1.Image != null) this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = myBitmap;
this.index++;
}
else
this.index=0;
}

private void Form_Click(object sender, EventArgs e)
{
this.Close();
}

}


屏幕截图实现核心代码:

void catcher_DoubleClick(object sender, EventArgs e)
{
this.catcher.TransparencyKey = this.BackColor;
Bitmap bmpCatched = new Bitmap(this.catcher.Width, this.catcher.Height);
Graphics g = Graphics.FromImage(bmpCatched);
Point locat = new Point(this.catcher.Location.X);
g.CopyFromScreen(this.catcher.Location, new Point(0, 0), this.catcher.ClientRectangle.Size, CopyPixelOperation.SourceCopy);
//Clipboard.SetImage(bmpCatched);
this.saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Image File(*.jpg;)|*.jpg;|Image File(*.bmp;)|*.bmp;";
saveFileDialog1.ShowDialog();

//If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
switch (saveFileDialog1.FilterIndex)
{
case 1:
bmpCatched.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;

case 2:
bmpCatched.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;

}

fs.Close();
}
this.catcher.Close();
g.Dispose();
this.catcher.Dispose();
this.Close();
}

部分功能实现思路:

1. 图片查看窗口中“实际大小”的功能通过使用Graphics.DrawImage(Image, destRectangle, srcRectangle, GraphicsUnit)的这个重载方法实现,destRectangle是确定画布的大小,里面可以自定义画布起始的坐标,长度和宽度;srcRectangle定义的是需要绘制的原图片的哪一个部分,里面有起始的坐标和图片的长度和宽度。
2. 在按下“实际大小”按钮之后,如果图片实际的分辨率比屏幕的分辨率大,则可以对图片进行拖拽,图片默认显示从(0,0)坐标开始到pictureBox的长和宽大小的部分。当进入可以拖拽的的pictureBox的区域,鼠标的指针会变成一个hand的形状,离开后恢复。
3. 幻灯片的功能通过新建一个没有边界,不显示状态栏图标的窗口完成,整个效果如同全屏显示图片。新建的窗口包含一个timer,里面设定Interval为1500,每过1500毫秒显示下一张图片,图片是循环显示的,通过鼠标点击结束幻灯片的播放。
4. 屏幕截图功能实现思路:
a) 首先新建一个没有边界,不显示状态栏图标的窗口formback。
b) 把formback的边界设置为系统窗口的边界
c) 把formback的背景图片设置为系统当前状态。获取系统当前状态的方式:通过使用Graphics.CopyFromScreen函数实现,起始位置(0,0),长度和宽度为屏幕的大小。
d) 当鼠标左键按下时新建另一个窗口,一个没有边界没有状态栏图标的窗口formcatcher。通过鼠标的移动,不断改变窗口的大小,当鼠标左键松开,formcatcher最终生成。鼠标滑过的地方有一层乳白色的半透明窗口,双击formcatcher,把窗体透明区域的颜色设置成为完全透明。
e) 再次使用Graphics.CopyFromScreen函数,截取由formcatcher选中的区域。弹出保存文件的窗口,填写文件名,保存文件。

遗憾的是还剩下一些Bug没有解决,那就是在图片查看窗口中,当图片经过旋转操作之后如果再使用“实际大小”的拖拽功能,图片可以被拖出图片窗口外面,原因很简单,因为重画的时候大小依然按照旋转前的图片的大小。

最后,需要说明的是这个程序中主窗口的系统文件树是在原始版是在下面这个地址找到的,后面我进行了一些改动:

/article/4689955.html

里面对treeView进行了很详尽的讲解。

还有,屏幕截图功能的实现是在下面地址中的qq截屏软件的基础上进行改进的:

/article/4664840.html

非常感谢上面的两位

因为实验还没有提交评分,所以程序的源码就先不上,先上一个程序的运行版,源代码后面会补上

今天修复了上面所说的bug,重新上传运行的程序

最后,上传上源码,因为后面没有时间做,很多bug没有调,希望多多包涵
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: