winform中图片的左移 右移并记忆移动的位置
2012-12-26 17:28
309 查看
上篇文章实现了winform 中图片的左旋 右旋,在此基础上增加 左移 右移 并记忆移动的位置。 并整合在一起发布。
上篇文章地址。/article/2448749.html
上贴部分核心代码:
Form1.cs 代码
PreviewControll.cs 自定义控件中代码
好了。代码都在这了。希望对各位能有所帮助,
下载地址:
http://files.cnblogs.com/kongwei521/WindowsFormsApplication.7z
上篇文章地址。/article/2448749.html
上贴部分核心代码:
Form1.cs 代码
private List<PreviewControl> poPreviews = null; public Form1() { InitializeComponent(); poPreviews = new List<PreviewControl>(); } /// <summary> /// 取得选择的图片 /// </summary> /// <returns></returns> private PreviewControl GetCurrentPreview() { for (int i = 0; i < poPreviews.Count; i++) { if (poPreviews[i].Selected) { return poPreviews[i]; } } return null; } /// <summary> /// 前移 /// </summary> /// <param name="eventSender"></param> /// <param name="eventArgs"></param> private void btnBackMove_Click(object sender, EventArgs e) { PreviewControl oCurrent = GetCurrentPreview(); if (oCurrent == null) { return; } PreviewControl oTemp = null; int iAt = poPreviews.IndexOf(oCurrent); //判断移动的位置 if (iAt >= 1) { oTemp = poPreviews[iAt - 1]; poPreviews.RemoveAt(iAt - 1); poPreviews.Insert(iAt, oTemp); } for (int i = 0; i < poPreviews.Count; i++) { pnlPreviews.Controls.SetChildIndex(poPreviews[i], i); } } /// <summary> /// 后移 /// </summary> /// <param name="eventSender"></param> /// <param name="eventArgs"></param> private void btnNextMove_Click(object sender, EventArgs e) { PreviewControl oCurrent = GetCurrentPreview(); if (oCurrent == null) { return; } PreviewControl oTemp = null; int iAt = poPreviews.IndexOf(oCurrent); if (iAt < poPreviews.Count - 1) { oTemp = poPreviews[iAt + 1]; poPreviews.RemoveAt(iAt + 1); poPreviews.Insert(iAt, oTemp); } for (int i = 0; i < poPreviews.Count; i++) { pnlPreviews.Controls.SetChildIndex(poPreviews[i], i); } }
PreviewControll.cs 自定义控件中代码
public partial class PreviewControl : UserControl { [DllImport("user32.dll")] extern static short GetKeyState(int vKey); /// <summary> /// 加载图片 /// </summary> private string psImageLocation = String.Empty; /// <summary> /// 选择状态 /// </summary> private bool pbSelected = false; /// <summary> /// 多个选择的状态 /// </summary> private bool pbMultiMode = false; /// <summary> /// 左旋,右旋后直接保存 /// false:不保存、 true:保存 /// </summary> private bool pbRotateSaveFlg = true; /// <summary> /// PDF文件类型的标志 /// TRUE:PDF文件 FALSE:不是PDF文件 /// </summary> private bool pbPdfFlg = false; /// <summary> /// 选择时的颜色 /// </summary> private Color poSelectColor = SystemColors.Highlight; /// <summary> /// 未选择的颜色 /// </summary> private Color poUnSelectColor = SystemColors.Control; /// <summary> /// 图片左旋右旋的值 /// </summary> private int piRoteSum = 0; /// <summary> /// 右击点击标签 /// </summary> private bool pbRightClickFlg = false; /// <summary> /// PictrueBoxのセレクトイベントハンドラー /// </summary> private event EventHandler poPictureSelected; /// <summary> /// 图片预览控件初始化 /// </summary> public PreviewControl() { InitializeComponent(); pictureBox1.Click -= new EventHandler(pictureBox1_Click); pictureBox1.Click += new EventHandler(pictureBox1_Click); pictureBox2.Click -= new EventHandler(pictureBox2_Click); pictureBox2.Click += new EventHandler(pictureBox2_Click); } private bool IsControlKeyDown() { return (GetKeyState(0x0011) & 0x0000FF00) != 0; } private bool IsShiftKeyDown() { return (GetKeyState(0x0010) & 0x0000FF00) != 0; } /// <summary> /// 右击的标签 /// </summary> public bool RightClickFlg { get { return pbRightClickFlg; } set { pbRightClickFlg = value; } } /// <summary> /// 加载的图片 /// </summary> public string ImageLocation { set { psImageLocation = value; if (psImageLocation.Length == 0) { pictureBox1.Visible = false; webBrowser1.Visible = false; } else if (value.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase)) { webBrowser1.Visible = true; webBrowser1.Navigate(value); pictureBox2.Tag = value; pictureBox2.Visible = true; pictureBox1.Visible = false; pbPdfFlg = true; } else { webBrowser1.Visible = false; pictureBox1.Visible = true; pictureBox1.ImageLocation = value; } } get { return psImageLocation; } } /// <summary> /// 选择时的颜色 /// </summary> public Color SelectColor { get { return poSelectColor; } set { poSelectColor = value; } } /// <summary> /// 没有选择的颜色 /// </summary> public Color UnSelectColor { get { return poUnSelectColor; } set { poUnSelectColor = value; } } /// <summary> /// 多选的场合为True /// </summary> public bool MultiMode { get { return pbMultiMode; } set { pbMultiMode = value; } } /// <summary> /// 多选 /// </summary> public bool MultiSelected { get { return pbSelected; } set { if (value == pbSelected) { return; } pbSelected = value; if (value) { BackColor = poSelectColor; } else { BackColor = poUnSelectColor; } } } /// <summary> /// 单选 /// </summary> public bool Selected { get { return pbSelected; } set { if (value == pbSelected) { if (!pbMultiMode) { return; } else { BackColor = poUnSelectColor; } } pbSelected = value; if (value) { BackColor = poSelectColor; Control oParent = Parent; if (oParent != null) { for (int i = 0; i < oParent.Controls.Count; i++) { if (oParent.Controls[i] != this && ((oParent.Controls[i] is PreviewControl) || (oParent.Controls[i] is WebBrowser)) ) { ((PreviewControl)oParent.Controls[i]).Selected = false; } } } } else { BackColor = poUnSelectColor; } } } /// <summary> /// 图片旋转的值入 90 180 270 度 /// </summary> public int RotateSum { get { return piRoteSum; } set { piRoteSum = value; } } /// <summary> /// 保存旋转后位置的状态 /// </summary> public bool RotateSaveFlg { set { pbRotateSaveFlg = value; } } /// <summary> /// 左旋 /// </summary> public void RotateLeft() { if (pictureBox1.Image != null) { //扩展名判断 if (CheckFileExtension(this.Tag.ToString()) == FileExtension.PDF) { return; } Image oSrc = pictureBox1.Image; Bitmap oDst = new Bitmap(oSrc); oDst.RotateFlip(RotateFlipType.Rotate270FlipNone); pictureBox1.Image = oDst; piRoteSum = piRoteSum + 270; if (!pbRotateSaveFlg) { return; } ImageSave((Bitmap)pictureBox1.Image, 100, pictureBox1.ImageLocation,0,0); } } /// <summary> /// 右旋 /// </summary> public void RotateRight() { if (pictureBox1.Image != null) { //扩展名判断 if (CheckFileExtension(this.Tag.ToString()) == FileExtension.PDF) { return; } Image oSrc = pictureBox1.Image; Bitmap oDst = new Bitmap(oSrc); oDst.RotateFlip(RotateFlipType.Rotate90FlipNone); pictureBox1.Image = oDst; piRoteSum = piRoteSum + 90; if (!pbRotateSaveFlg) { return; } ImageSave((Bitmap)pictureBox1.Image, 100, pictureBox1.ImageLocation,0,0); } } /// <summary> /// 清除图片的记忆位置 /// </summary> public void ClearImageMemory() { if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); } if (pictureBox2.Image != null) { pictureBox2.Image.Dispose(); } if (!webBrowser1.IsDisposed) { webBrowser1.Dispose(); } } /// <summary> /// 利用GC回收释放内存 /// </summary> public void GCCollect() { pictureBox1.Image = null; pictureBox2.Image = null; webBrowser1 = null; } private void pictureBox2_Click(object sender, EventArgs e) { PictureBoxClick(sender, e); } private void pictureBox1_Click(object sender, EventArgs e) { PictureBoxClick(sender, e); } private void PictureBoxClick(object sender, EventArgs e) { if (pbMultiMode) { if (IsControlKeyDown()) { MultiSelected = !MultiSelected; } else if (IsShiftKeyDown()) { Control oParent = Parent; if (oParent != null) { int iFromIndex = 0; //选择图片的标签 bool bHadSelected = false; for (int i = 0; i < oParent.Controls.Count; i++) { if (oParent.Controls[i] != this && oParent.Controls[i] is PreviewControl && oParent.Controls[i].BackColor == SelectColor) { bHadSelected = true; iFromIndex = i; break; } } int iToIndex = 0; for (int j = 0; j < oParent.Controls.Count; j++) { if (oParent.Controls[j] == this && oParent.Controls[j] is PreviewControl) { iToIndex = j; break; } } if (!bHadSelected) { iFromIndex = iToIndex; } if (iToIndex < iFromIndex) { int temp = iFromIndex; iFromIndex = iToIndex; iToIndex = temp; } for (int k = iFromIndex; k <= iToIndex; k++) { if (oParent.Controls[k] is PreviewControl) { oParent.Controls[k].BackColor = SelectColor; ((PreviewControl)oParent.Controls[k]).MultiSelected = true; } } } } else { Selected = true; } } else { Selected = true; if (poPictureSelected != null) { poPictureSelected(sender, e); } } } #region 文件扩展名格式check public static FileExtension CheckFileExtension(string fileName) { if (!File.Exists(fileName)) { return FileExtension.VALIDFILE; } FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); string fileType = string.Empty; FileExtension extension = FileExtension.VALIDFILE; try { byte data = br.ReadByte(); fileType += data.ToString(); data = br.ReadByte(); fileType += data.ToString(); extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType); if (extension.ToString().Equals(fileType)) { extension = FileExtension.VALIDFILE; } } catch { extension = FileExtension.VALIDFILE; } finally { if (fs != null) { fs.Close(); br.Close(); } } return extension; } //文件枚举类型 public enum FileExtension { JPEG = 255216, TIF = 7373, GIF = 7173, BMP = 6677, PNG = 13780, PDF = 3780, VALIDFILE = 9999999 } #endregion /// <summary> /// 保存图片 /// </summary> /// <param name="src">图片路径</param> /// <param name="scale">比例</param> /// <param name="asSaveFile"></param> /// <param name="aiWidth">宽度</param> /// <param name="aiHeight">高度</param> public static void ImageSave(Bitmap src, int scale, string asSaveFile, int aiWidth, int aiHeight) { int w = src.Width; int h = src.Height; if (scale < 100) { w = aiWidth; h = aiHeight; } Bitmap dest = new Bitmap(w, h); Graphics g = Graphics.FromImage(dest); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.High; g.DrawImage(src, 0, 0, w, h); g.Dispose(); dest.Save(asSaveFile, ImageFormat.Jpeg); dest.Dispose(); } }
好了。代码都在这了。希望对各位能有所帮助,
下载地址:
http://files.cnblogs.com/kongwei521/WindowsFormsApplication.7z
相关文章推荐
- winform 鼠标拖动移动图片位置
- winForm pictureBox拖动图片and动态new 窗体&移动Panel窗体头
- Javascript实现图片位置控制(鼠标拖拽 + 键盘方向键移动)源码分享
- 移动滚动轴,背景图片位置不变。
- 在WPF里面实现以鼠标位置为中心缩放移动图片
- 图片的切换与位置的移动
- C#实现winform中RichTextBox在指定光标位置插入图片的方法
- android 控件的位置移动处理,利用bitmap来不断更新图片位置
- 让图片直线移动到鼠标点击位置,类似游戏里的人物走动!
- 移动View位置时,修改图片资源导致View在原位置闪烁的问题
- 双击放大图片 手指移动图片位置
- 图片跟随鼠标移动(期间鼠标不释放)以及释放鼠标(图片)时刻鼠标位置
- 让图片直线移动到鼠标点击位置,类似游戏里的人物走动!
- 我用setX()设置图片的位置,图片刚移动就位置还原了,求大神帮助
- 鼠标移动到一个位置弹出相应的一张图片
- 鼠标移动到一个位置,显示图片(类似qq的隐藏显示功能)
- Android 开发知识 图片跟随触摸位置移动
- winform中RichTextBox在指定光标位置插入图片
- 点击图片随机移动位置
- HTML 里面的图片怎么移动位置?