http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3592|回复: 2

[其他] c# 操作图片

[复制链接]
发表于 2012-11-13 11:33:17 | 显示全部楼层 |阅读模式
在winfrom的pictureBox里怎样实现图片的放大和缩小。。(用两个按钮,一个放大,一个缩小)        
解决方法
[code=csharp]private void scalePic()
        {
            try
            {
                float fx = (float)iPercent / 100;
                float fy = (float)iPercent / 100;

                int tmpX = ImgSrc.Width * iPercent;
                int tmpY = ImgSrc.Height * iPercent;
                int x = 0;
                int y = 0;

                if (tmpX / 100 < panel1.Width)
                    x = (panel1.Width - tmpX / 100) / 2;
                if (tmpY / 100 < panel1.Height)
                    y = (panel1.Height - tmpY / 100) / 2;

                Image tmpImg = new Bitmap(ImgSrc, tmpX / 100, tmpY / 100);
                using (Graphics g = Graphics.FromImage(tmpImg))
                {
                    g.ScaleTransform(fx, fy);
                    g.DrawImage(tmpImg, 0, 0);
                }
                picShow.Left = x;//pictureBox控件
                picShow.Top = y;
                picShow.Image = tmpImg;

                txtPercent.Text = string.Format("{0}%", iPercent);

                picShow.Anchor = AnchorStyles.Left | AnchorStyles.Top;
                picShow.SizeMode = PictureBoxSizeMode.AutoSize;
            }
            catch (Exception ex)
            {
                 
            }
        }[/code]


本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2012-11-13 11:33:36 | 显示全部楼层
[code=csharp]/// <summary>
        /// 图像缩放
        /// </summary>
        /// <param name="dx">横向缩放比列</param>
        /// <param name="dy">纵向缩放比列</param>
        private void ScalePictureBox(float dx,float dy)
        {
            this.pictureBox1.Location = new Point(0, 0);
            this.panel1.AutoScrollPosition = new Point(0, 0);
            SizeF sf = new SizeF(dx, dy);
            this.pictureBox1.Scale(sf);
        }
        /// <summary>
        /// 图像缩放比列及放大或缩小幅大
        /// </summary>
        /// <param name="dx">横向缩放比列</param>
        /// <param name="dy">纵向缩放比列</param>
        /// <param name="zoomFlag">放大缩小标志,false代表缩小,true代表放大</param>
        private void ZoomImage(  float dx,  float dy,bool zoomFlag)
        {
            //放大到5倍时停止放大
            if (zoomFlag)
            {
                if (this.pictureBox1 .Width  >= 5 * this.pictureBox1.Image.Width && this.pictureBox1 .Height>= 5 * this.pictureBox1.Image.Height)
                    return;
                 ScalePictureBox(dx,dy);
             }
            //图像的宽或高大于等于panel的宽或高时,则缩小到填充整个panel时为止,否则缩小到原图大小
            else  
            {
                //图像比panel大,且已缩小到小于等于panel时为止
                if ((this.pictureBox1.Image.Width >= this.panel1.Width || this.pictureBox1.Image.Height >= this.panel1.Height)
                    && (this.pictureBox1.Width <= this.panel1.Width && this.pictureBox1.Height <= this.panel1.Height))
                {
                    return;
                }
                //图像比panel小
                else if (this.pictureBox1.Image.Width < this.panel1.Width && this.pictureBox1.Image.Height < this.panel1.Height)
                {
                    //已缩小到原图大小时为止
                    if (this.pictureBox1.Width <= this.pictureBox1.Image.Width || this.pictureBox1.Height <= this.pictureBox1.Image.Height)
                    {
                        return;
                    }
                    ScalePictureBox(dx, dy);
                }
                //图像比panel大,且仍可缩小到填充整个panel
                else
                {
                    ScalePictureBox(dx, dy);
                    if (this.pictureBox1.Width <= this.panel1.Width && this.pictureBox1.Height <= this.panel1.Height)
                    {
                        this.pictureBox1.Width = this.panel1.Width;
                        this.pictureBox1.Height = this.panel1.Height;
                    }
                }
            }
        }[/code]
发表于 2013-10-22 21:10:14 | 显示全部楼层
踩踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-3 17:56

© 2014-2021

快速回复 返回顶部 返回列表