最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

winform中实现图片左旋,右旋

IT圈 admin 37浏览 0评论

winform中实现图片左旋,右旋

左旋

 

继续旋转

右旋:

继续右旋

效果展示结束:

下面讲讲利用什么来实现的:

由于不光判断是否是图片的*.BMP;*.JPG;*.GIF;*.TIF;*.PNG 还有*.PDF文件,因此写了个自定义控件里面处理左旋,右旋,背景颜色,之类的。这里不来实现PDF

ImageList.cs里面的处理:

private List<PreviewControl> poPreviews = null;public ImageList(){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>public void cmdLeftRoteImage_Click(System.Object eventSender, System.EventArgs eventArgs){PreviewControl oCurrent = GetCurrentPreview();if (oCurrent != null){oCurrent.RotateLeft();}}/// <summary>/// 右旋/// </summary>/// <param name="eventSender"></param>/// <param name="eventArgs"></param>public void cmdRightRoteImage_Click(System.Object eventSender, System.EventArgs eventArgs){PreviewControl oCurrent = GetCurrentPreview();if (oCurrent != null){oCurrent.RotateRight();}}

PreviewControll.cs 的代码: 自定义控件上面拖放PictureBox 和WebBrowser

 [DllImport("user32.dll")]extern static short GetKeyState(int vKey);/// <summary>/// 加载图片/// </summary>private string psImageLocation = String.Empty;/// <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>public PreviewControl(){InitializeComponent();                      }private bool IsControlKeyDown(){return (GetKeyState(0x0011) & 0x0000FF00) != 0;}private bool IsShiftKeyDown(){return (GetKeyState(0x0010) & 0x0000FF00) != 0;}/// <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>/// 图片旋转的值入 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()) == GFBEFUNC.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()) == GFBEFUNC.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;}
  /// <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();}
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}


贴出来的核心代码:

winform中实现图片左旋,右旋

左旋

 

继续旋转

右旋:

继续右旋

效果展示结束:

下面讲讲利用什么来实现的:

由于不光判断是否是图片的*.BMP;*.JPG;*.GIF;*.TIF;*.PNG 还有*.PDF文件,因此写了个自定义控件里面处理左旋,右旋,背景颜色,之类的。这里不来实现PDF

ImageList.cs里面的处理:

private List<PreviewControl> poPreviews = null;public ImageList(){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>public void cmdLeftRoteImage_Click(System.Object eventSender, System.EventArgs eventArgs){PreviewControl oCurrent = GetCurrentPreview();if (oCurrent != null){oCurrent.RotateLeft();}}/// <summary>/// 右旋/// </summary>/// <param name="eventSender"></param>/// <param name="eventArgs"></param>public void cmdRightRoteImage_Click(System.Object eventSender, System.EventArgs eventArgs){PreviewControl oCurrent = GetCurrentPreview();if (oCurrent != null){oCurrent.RotateRight();}}

PreviewControll.cs 的代码: 自定义控件上面拖放PictureBox 和WebBrowser

 [DllImport("user32.dll")]extern static short GetKeyState(int vKey);/// <summary>/// 加载图片/// </summary>private string psImageLocation = String.Empty;/// <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>public PreviewControl(){InitializeComponent();                      }private bool IsControlKeyDown(){return (GetKeyState(0x0011) & 0x0000FF00) != 0;}private bool IsShiftKeyDown(){return (GetKeyState(0x0010) & 0x0000FF00) != 0;}/// <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>/// 图片旋转的值入 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()) == GFBEFUNC.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()) == GFBEFUNC.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;}
  /// <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();}
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}


贴出来的核心代码:

发布评论

评论列表 (0)

  1. 暂无评论