C截图拖拽矩形图片局部放大模仿淘宝

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C#截图、拖拽矩形图片局部放大模仿淘宝(2012-10-2411:15:28)转载▼标签:it效果图:总共就两个控件,大的是pictureBox1,小的是pictureBox2:需要说一下的是,bitmap.Clone的方法如果操作不当会导致内存不足的错误。当鼠标截图的时候,截图的坐标大于边界的时候,就会内存不足的错误,所以解决办法就是拖到边界的时候,就不再扩大了,鼠标松开的时候截图的区域就是边界,试一下就知道;还有就是及时利用垃圾回收机制GC.Collect()清理垃圾。哦对了,图片自己找一下吧,别忘了自己手动打一遍代码:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespacePicZoom{publicpartialclassForm1:Form{publicstaticinti=0;privatePointm_ptStart=newPoint(0,0);privatePointm_ptEnd=newPoint(0,0);privateboolm_bMouseDown=false;privatefloatxRate,yRate,realX1,realY1,realX2,realY2;intpLeft=0;intpTop=0;publicForm1(){InitializeComponent();}privatevoidpictureBox1_MouseDown(objectsender,MouseEventArgse){if(pictureBox1.HasChildren){for(inti=0;ipictureBox1.Controls.Count;i++){pictureBox1.Controls.RemoveAt(0);}}if(e.Button!=MouseButtons.Left){return;}m_ptEnd=newPoint(e.X,e.Y);this.pictureBox1.Refresh();realX1=e.X*xRate;realY1=e.Y*yRate;if(!m_bMouseDown){m_ptStart=newPoint(e.X,e.Y);m_ptEnd=newPoint(e.X,e.Y);}m_bMouseDown=!m_bMouseDown;}privatevoidpictureBox1_Paint(objectsender,PaintEventArgse){if(m_ptEnd.X-m_ptStart.X0||m_ptEnd.Y-m_ptStart.Y0){return;}if(m_ptEnd.X-m_ptStart.X=100){m_ptEnd.X=m_ptStart.X+100;}if(m_ptEnd.Y-m_ptStart.Y=100){m_ptEnd.Y=m_ptStart.Y+100;}e.Graphics.DrawRectangle(System.Drawing.Pens.Blue,m_ptStart.X,m_ptStart.Y,m_ptEnd.X-m_ptStart.X,m_ptEnd.Y-m_ptStart.Y);}privatevoidpictureBox1_MouseUp(objectsender,MouseEventArgse){inteX=0,eY=0;if(e.Button!=MouseButtons.Left){return;}if(e.X-m_ptStart.X=100){if(e.X=pictureBox1.Width-1){if(pictureBox1.Width-m_ptStart.X-1100){eX=m_ptStart.X+100;}else{eX=pictureBox1.Width-1;}}else{eX=m_ptStart.X+100;}}else{if(e.X=pictureBox1.Width-1){eX=pictureBox1.Width-1;}else{eX=e.X;}}if(e.Y-m_ptStart.Y=100){if(e.Y=pictureBox1.Height-1){if(pictureBox1.Height-m_ptStart.Y-1100){eX=m_ptStart.Y+100;}else{eY=pictureBox1.Height-1;}}else{eY=m_ptStart.Y+100;}}else{if(e.Y=pictureBox1.Height-1){eY=pictureBox1.Height-1;}else{eY=e.Y;}}if(m_ptStart.X=0&&m_ptEnd.X=0&&m_ptStart.Y=0&&m_ptEnd.Y=0&&m_ptStart.X=254&&m_ptEnd.X=254&&m_ptStart.Y=163&&m_ptEnd.Y=163){m_ptEnd=newPoint(eX,eY);m_bMouseDown=!m_bMouseDown;this.pictureBox1.Refresh();}else{m_ptEnd=newPoint(eX,eY);m_ptEnd=m_ptStart;m_bMouseDown=!m_bMouseDown;this.pictureBox1.Refresh();}realX2=eX*xRate;realY2=eY*yRate;Crop((Bitmap)pictureBox1.Image);Panelp=newPanel();p.Name=panel1;p.Location=newPoint((int)(realX1/xRate),(int)(realY1/yRate));p.Size=newSize((int)(realX2/xRate-realX1/xRate),(int)(realY2/yRate-realY1/yRate));//p.BackColor=Color.Transparent;p.BackColor=Color.FromArgb(100,135,206,250);//Azure240255255p.BorderStyle=BorderStyle.FixedSingle;p.MouseDown+=(s1,e1)={pLeft=e1.X;pTop=e1.Y;};p.MouseMove+=(s2,e2)={GC.Collect();if(e2.Button.ToString().Equals(Left)){if(p.Location.X+e2.X-pLeft=1){p.Left=1;}elseif(p.Location.X+e2.X-pLeft=pictureBox1.Width-p.Width){p.Left=pictureBox1.Width-p.Width-1;}else{p.Left=p.Location.X+e2.X-pLeft;}if(p.Location.Y+e2.Y-pTop=1){p.Top=1;}elseif(p.Location.Y+e2.Y-pTop=pictureBox1.Height-p.Height){p.Top=pictureBox1.Height-p.Height-1;}else{p.Top=p.Location.Y+e2.Y-pTop;}}Crop((Bitmap)pictureBox1.Image,Convert.ToInt32(p.Location.X*xRate),Convert.ToInt32(p.Location.Y*yRate),Convert.ToInt32(p.Width),Convert.ToInt32(p.Height));};pictureBox1.Controls.Add(p);}privatevoidpictureBox1_MouseMove(objectsender,MouseEventArgse){if(e.Button!=MouseButtons.Left){return;}m_ptEnd=newPoint(e.X,e.Y);this.pictureBox1.Refresh();}privatevoidForm1_Load(objectsender,EventArgse){xRate=(float)pictureBox1.Image.Width/pictureBox1.Width;yRate=(float)pictureBox1.Image.Height/pictureBox1.Height;SetStyle(ControlStyles.UserPaint,true);SetStyle(ControlStyles.AllPaintingInWmPaint,true);//禁止擦除背景.SetStyle(ControlStyles.DoubleBuffer,true);//双缓冲}privatevoidCrop(Bitmapbitmap){if((int)(realX2-realX1)0&&(int)(realY2-realY1)0){GC.Collect();//GC.WaitForPendingFinalizers();Rectanglerec=newRectangle((int)realX1,(int)realY1,(int)(realX2-realX1),(int)(realY2-realY1));pictureBox2.Image=bitmap.Clone(rec,System.Drawing.Imaging.PixelFormat.Format32bppArgb);}}privatevoidCrop(Bitmapbitmap,intX,intY,intwidth,intheight){if(width0&&height0){Rectanglerec=newRectangle(X,Y,width,height);try{GC.Collect();//GC.WaitForPendingFinalizers();pictureBox2.Image=bitmap.Clone(rec,System.Drawing.Imaging.PixelFormat.Format32bppArgb);//bitmap.Dispose();}catch(Exceptionex){i++;}finally{}}}}}

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功