新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C# 图片截取 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7375 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C# 图片截取 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 C# 图片截取


    代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Windows.Forms;
    using System.Reflection;
    /*
    *  实现图片剪切
    *
    */
    namespace CutImage
    {
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }


            /// <summary>
            /// 打开图片
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnOpen_Click(object sender, EventArgs e)
            {
                string strFilename = "";
                OpenFileDialog dlgOpen = null;
                try
                {
                    dlgOpen = new OpenFileDialog();
                    dlgOpen.Filter = "All Image Files(*.*)|*.*";
                    if (dlgOpen.ShowDialog() == DialogResult.OK)
                    {
                        if (dlgOpen.FileName == "") return;
                        strFilename = dlgOpen.FileName;
                        pbxOriImage.Image = Image.FromFile(strFilename);
                        this.SetImageSizeMode(pbxOriImage);                   
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("无法识别的图片格式(" + strFilename + ")\r\n" + ex.Message , "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (dlgOpen != null)
                        dlgOpen.Dispose();
                    dlgOpen = null;
                }
            }

            /// <summary>
            /// 设置图片显示模式
            /// </summary>
            /// <param name="pImage"></param>
            private void SetImageSizeMode(PictureBox pImage)
            {
                if (pImage == null) return;
                Image imgResult = pImage.Image;
                if (imgResult != null)
                {
                    //控制图片显示方式
                    if (imgResult.Width > pImage.Width || imgResult.Height > pImage.Height)
                    {
                        pImage.SizeMode = PictureBoxSizeMode.Zoom;
                    }
                    else
                    {
                        pImage.SizeMode = PictureBoxSizeMode.CenterImage;
                    }
                }
            }

            #region 绘制及剪切
            Point fPtStart;                     //开始位置矩形的位置
            bool fHaveImage = false;            //是否有图片
            Point fPtFirst;                     //鼠标按下去的位置
            Point fPtSecond;                    //鼠标当前位置

            /// <summary>
            /// 记录鼠标按下去的位置
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void pbxOriImage_MouseDown(object sender, MouseEventArgs e)
            {
                this.Cursor = Cursors.Cross;
                fPtFirst = new Point(e.X, e.Y);
                fPtStart = new Point(e.X, e.Y);
                if ((sender as PictureBox).Image != null)
                {
                    this.fHaveImage = true;
                }
                else
                {
                    this.fHaveImage = false;
                }
            }

            private void pbxOriImage_MouseMove(object sender, MouseEventArgs e)
            {
                if (this.Cursor == Cursors.Cross)
                {
                    fPtSecond = new Point(e.X, e.Y);


                    if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
                    {
                        //鼠标当前位置在,按下去位置的右下角
                        fPtStart = new Point(fPtFirst.X, fPtFirst.Y);
                    }
                    else if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) < 0)
                    {
                        //鼠标当前位置在,按下去位置的右上角
                        fPtStart = new Point(fPtFirst.X, fPtSecond.Y);
                    }
                    else if ((fPtSecond.X - fPtFirst.X) < 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
                    {
                        //鼠标当前位置在,按下去位置的左下角
                        fPtStart = new Point(fPtSecond.X, fPtFirst.Y);
                    }
                    else
                    {
                        //鼠标当前位置在,按下去位置的左上角
                        fPtStart = new Point(fPtSecond.X, fPtSecond.Y);
                    }               
                    pbxOriImage.Invalidate();       //移动时绘制矩形选择框
                }
            }

            /// <summary>
            /// 剪切图片
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void pbxOriImage_MouseUp(object sender, MouseEventArgs e)
            {
                if (this.fHaveImage == false) return;
                try
                {
                    fPtSecond = new Point(e.X, e.Y);
                    if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
                    {
                        //鼠标当前位置在,按下去位置的右下角
                        fPtStart = new Point(fPtFirst.X, fPtFirst.Y);
                    }
                    else if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) < 0)
                    {
                        //鼠标当前位置在,按下去位置的右上角
                        fPtStart = new Point(fPtFirst.X, fPtSecond.Y);
                    }
                    else if ((fPtSecond.X - fPtFirst.X) < 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
                    {
                        //鼠标当前位置在,按下去位置的左下角
                        fPtStart = new Point(fPtSecond.X, fPtFirst.Y);
                    }
                    else
                    {
                        //鼠标当前位置在,按下去位置的左上角
                        fPtStart = new Point(fPtSecond.X, fPtSecond.Y);
                    }

                    Rectangle rectDisplay = this.GetPictureDisplaySize(pbxOriImage);        //图片实际显示的大小


                    int Width = Math.Abs((fPtSecond.X - fPtFirst.X));
                    int Height = Math.Abs((fPtSecond.Y - fPtFirst.Y));

                    double dWRate = pbxOriImage.Image.Width / (double)rectDisplay.Width;    //缩放比例:宽
                    double dHRate = pbxOriImage.Image.Height / (double)rectDisplay.Height;  //缩放比例:高

                    int intRealWidth = (int)(dWRate * Width);           //实际需要截取的图片宽度
                    int intRealHeight = (int)(dHRate * Height);         //实际需要截取的图片高度

                    int intRealX = (int)((fPtStart.X - rectDisplay.X) * dWRate); //实际的X坐标
                    int intRealY = (int)((fPtStart.Y - rectDisplay.Y) * dHRate); //实际的Y坐标

                    Bitmap bmpDest = new Bitmap(intRealWidth, intRealHeight, PixelFormat.Format32bppRgb);       //目标图片大小

                    Graphics g = Graphics.FromImage(bmpDest);               //创建GDI

                    Rectangle rectDest = new Rectangle(0, 0, intRealWidth, intRealHeight);
                    Rectangle rectSource = new Rectangle(intRealX,intRealY, intRealWidth, intRealHeight);

                    g.DrawImage(pbxOriImage.Image, rectDest, rectSource, GraphicsUnit.Pixel);               //绘图

                    pbxCutImage.Image = (Image)bmpDest;

                    g.Dispose();

                    SetImageSizeMode(pbxCutImage);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                { this.Cursor = Cursors.Default; }            
            }

            /// <summary>
            /// 获取图片实际显示的大小
            /// </summary>
            /// <param name="pbxImage"></param>
            /// <returns></returns>
            public Rectangle GetPictureDisplaySize(PictureBox pbxImage)
            {
                if (pbxImage != null)
                {
                    PropertyInfo ppiImageRect = pbxImage.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
                    return (Rectangle)ppiImageRect.GetValue(pbxImage, null);
                }
                return new Rectangle(0, 0, 1, 1);
            }

            /// <summary>
            /// 绘制矩形选择框
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void pbxOriImage_Paint(object sender, PaintEventArgs e)
            {
                int intWidth = 0;
                int intHeight = 0;
                if (this.fHaveImage == true)
                {
                    Pen p = new Pen(Color.Black, 1);
                    p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

                    intWidth = Math.Abs(fPtFirst.X - fPtSecond.X);
                    intHeight = Math.Abs(fPtFirst.Y - fPtSecond.Y);                         

                    Rectangle rectDraw = new Rectangle(fPtStart, new Size(intWidth, intHeight));
                    e.Graphics.DrawRectangle(p, rectDraw);
                }
            }

            #endregion
            /// <summary>
            /// 保存剪切后的图片
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSave_Click(object sender, EventArgs e)
            {
                SaveFileDialog dlgSave = new SaveFileDialog();

                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    pbxCutImage.Image.Save(dlgSave.FileName);
                }
            }
        }
    }


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2014/7/11 9:25:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客2
    发贴心情 
    我有个图片存在bitmap变量里。由于显示的需要,想把图片左边330的像素宽度的图片放在label中,右边的其他部分放在label2中,请问怎么截取

    将整个图片读取,然后找出你要的部分画到需要的位置!
    Graphics可以完成你要的功能
    bitmap bit = new Bitmap("222.bmp")
    //222还要获取图片的大小Hight\Width
    rectangle rect = rectangle(0,0,宽,高)
    //这里是得到一个矩形
    graphics.DrawImage(bit ,rect);
    //将矩形大小的图片重绘,想绘到哪就连到哪!

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2014/7/11 9:27:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/26 12:56:43

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    109.375ms