如何在窗体编程和网页编程中按图片比例显示缩放后的图片(.net 2005)

C#制作一个图片查看器,具有滚轮放大缩小,鼠标拖动,图像像素化,显示颜色RGB信息功能 使用C#制作一个图片查看器,具有滚轮放大缩小,鼠标拖动,图像像素化,显示颜色RGB信息功能。 阅读详情

            直接从文件读取出来的图片跟显示的控件大小并不成比例,如果在窗体编程中直接让PictureBox来显示就会按PictureBox的大小截断显示,会丢失部分图片或控件部分空白;如果在网页编程中直接让Image控件或ImageButton控件显示就会按控件的比例填满,造成图片变形。

下面讲讲在两种方式编程中既不会令图片部分丢失又不会令图片变形的方法。
窗体编程比较简单了,只要写一个函数把图片按长宽转换一下,再把转换后的图片显示在控件上就OK了,函数定义如下:
///<summary>
        ///按比例缩放图片
        ///</summary>
        ///<param name="SourceImage"></param>
        ///<param name="TargetWidth"></param>
        ///<param name="TargetHeight"></param>
        private Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight)
        {
            int IntWidth; //新的图片宽
            int IntHeight; //新的图片高
            try
            {
                System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat;
                System.Drawing.Bitmap SaveImage = new System.Drawing.Bitmap(TargetWidth, TargetHeight);
                Graphics g = Graphics.FromImage(SaveImage);
                g.Clear(Color.White);
 
                //计算缩放图片的大小
 
                if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小
                {
                    IntWidth = TargetWidth;
                    IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
                }
                else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大
                {
                    IntHeight = TargetHeight;
                    IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
                }
                else//长宽比目的图片长宽小或大
                {
                    IntWidth = TargetWidth;
                    IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
                    if (IntHeight > TargetHeight)//重新计算
                    {
                        IntHeight = TargetHeight;
                        IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
                    }
                }
 
                g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight);
                SourceImage.Dispose();
               
                return SaveImage;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
从磁盘读取图片到内存,并显示在控件pictureBox1上:
          Image im = Image.FromFile(FileName);
  this.pictureBox1.Image = this.ZoomPicture(im,this.pictureBox1.Width,pictureBox1.Height);
当然使用PictureBox的SizeMode属性令它等于PictureBoxSizeMode.Zoom可以达到同样的显示效果,但是如果将图片存储到数据库什么的,就可以通过这个函数来转换后在存储,使所有图片的大小一致。
 
不过要在网页里显示按比例缩放的图片就比较复杂了,首先新建一个叫DisplayImage.aspx 的WebForm,编辑函数:
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            int nWidth, nHeight;
            string ImagePath = Page.Request.QueryString["ImagePath"];
            string strwidth = Page.Request.QueryString["Width"];
            string strheight = Page.Request.QueryString["Height"];
            try
            {
                if (strwidth == string.Empty || strheight == string.Empty)
                {
                    nWidth = 150;
                    nHeight = 150;
                }
                else
                {
                    nWidth = Convert.ToInt32(strwidth);
                    nHeight = Convert.ToInt32(strheight);
                }
            }
            catch
            {
                nWidth = 150;
                nHeight = 150;
            }
            ImageManager.DisplayPicture(Server.MapPath(ImagePath), nWidth, nHeight,Page.Response);
        }
        catch
        {
        }
}
 
 
函数DisplayPicture是ImageManager的一个静态方法,在这个函数计算图片的缩放并把文件输出到网页,其定义如下:
///<summary>
    ///按指定长宽显示指定路径的图片
    ///</summary>
    ///<param name="SourceImagestr"></param>
    ///<param name="TargetWidth"></param>
    ///<param name="TargetHeight"></param>
    public static void DisplayPicture(string SourceImagestr, int TargetWidth, int TargetHeight, HttpResponse Response)
    {
        int IntWidth; //新的图片宽
        int IntHeight; //新的图片高
        System.Drawing.Image SourceImage; //来源图片定义
        try
        {
            SourceImage = System.Drawing.Image.FromFile(SourceImagestr);
            System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat;
            System.Drawing.Bitmap SaveImage = new System.Drawing.Bitmap(TargetWidth, TargetHeight);
            Graphics g = Graphics.FromImage(SaveImage);
            g.Clear(Color.White);
 
            //计算缩放图片的大小
 
            if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小
            {
                IntWidth = TargetWidth;
                IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
            }
            else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大
            {
                IntHeight = TargetHeight;
                IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
            }
            else//长宽比目的图片长宽小或大
            {
                IntWidth = TargetWidth;
                IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
                if (IntHeight > TargetHeight)//重新计算
                {
                    IntHeight = TargetHeight;
                    IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
                }
            }
            g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight);
            SourceImage.Dispose();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            SaveImage.Save(ms, format);
            Response.ClearContent();
            Response.ContentType = "image/"+format.ToString();
            Response.BinaryWrite(ms.ToArray());
        }
        catch (Exception ex)
        {
            throw ex;
        }
}
 
在要显示图片的网页加一个Image或ImageButton控件,假设已加的控件是ImageButton1,代码为:
<asp:ImageButton ID="ImageButton1" ImageUrl='<%# "DisplayImage.aspx?ImagePath=~/Upload/MyPicture.gif&Width=100&Height=100" %>' runat="server" Height="100px" Width="100px" />
这样在这个ImageButton控件里就可以显示按图片比例大小缩放后能在控件显示的最大图片了,Image控件使用方法相同,如果显示图片是从服务端代码获取只要把ImageUrl='<%# "DisplayImage.aspx?ImagePath=~/Upload/MyPicture.gif&Width=100&Height=100" %>'改成ImageUrl='<%# "DisplayImage.aspx?ImagePath="  + Eval("PicAddress") +  "&Width=100&Height=100" %>'就可以了。
图片显示的大小参数可以自己设置。
 
C#打开图片显示pictureBox中(适应其大小并保持原始比例 //创建OpenFileDialog OpenFileDialog opnDlg = new OpenFileDialog(); //为图像选择一个筛选器 opnDlg.Filter = "所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;" + "*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|" + ... 阅读详情

相关推荐

使用C#的WinForm中的PictureBox控件实现图片显示

在上面的代码中,我们创建了一个名为pictureBox1的PictureBox控件,并设置了它的位置、大小SizeMode属性。需要注意的是,在使用PictureBox控件显示图片时,需要确保图片的路径是正确的,并且程序具有访问该路径的权限。在C#的WinForm应用程序开发中,PictureBox控件是一个常用的工具,用于在窗体显示图像。在这个例子中,"image"是资源文件中的图片名称,通过Properties.Resources访问资源文件,并将图片赋值给pictureBox1的Image属性。

qq_39605374的博客 2338

Winform(C#) 图片放大、缩小、打印导出

示例很简单,只包含图片放大、缩小、打印导出功能

C# PictureBox图片放大缩小、旋转、打印

C# PictureBox图片放大缩小、旋转、打印

c# Winform实现图片放大显示功能, 关闭放大窗口图片原样显示

首先,创建2个Form窗体,各放一个pictureBox.pictureBox的属性调整为:StretchImage。form1代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;u...

chulijun3107的博客 1万+

vb/vb.net开发精粹(22)

关于 VB 里其它过程的值传递给 Function 函数体的问题??? VB更改Excel自定义纸张大小怎么操作呢? 为什么我写的程序在VB环境下运行不报错,一编译运行就报错 数据导入EXCEL问题 发个预言贴, 过一年再回来看看是否变现.有兴趣的近来凑热闹~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ excel VBA 怎么用代码 指定下拉框的选中项 vb6对64X的

师以长技以制夷 913

一步一步玩控件:自定义TabControl——从山寨Safari开始

作者:野比 (conmajia@gmail.com) 时间:May, 2012 封面图片为野比原创,请勿未经允许私自引用 #1-1 嗯,各位,又是我,生物钟颠倒的家伙。 今天我要山寨的是大名鼎鼎的Apple,传说中的「被山寨之王」。 没错,都被我山寨好几次了。 说起Apple,相信大家对他家的各种产品,不管他软还是硬,都有相当的好感。 最近Apple把自家...

weixin_30472035的博客 73

[转]一步一步玩控件:自定义TabControl——从山寨Safari开始

作者:野比 (conmajia@gmail.com) 时间:May, 2012 封面图片为野比原创,请勿未经允许私自引用 #1-1 嗯,各位,又是我,生物钟颠倒的家伙。 今天我要山寨的是大名鼎鼎的Apple,传说中的「被山寨之王」。 没错,都被我山寨好几次了。 说起Apple,相信大家对他家的各种产品,不管他软还是硬,都有相当的好感。 最近Apple把自家的Web浏...

weixin_34204722的博客 135

c# winform 解决PictureBox 无法打印全部图片的问题

作者:沐汐 Vicky 出处:http://www.cnblogs.com/EasyInvoice 一、 问题描述 在页面使用PictureBox 加载资料图片后,点击“打印”,只能打印图片首页,较大图片则无法全部打印。 二、 原因分析 PictureBox打印图片时没有设置继续打印相关属性,因此每次只能打印第1页。 三、解决方法 PictureBox控件增加打印全部页面属性,如果为True,表示打印c#教程全部页面;如果为False,保留原有逻辑不变。 在打印全部页面时,将控件的图片按页面大小切

chinaherolts2008的博客 918

C#图片添加到窗体并等比例缩放

关于c#中将图片添加到窗体并实现等比例缩放

C#任意比例缩放显示图片_源代码

图像缩放显示 图片缩放 调节图像大小 图像处理 C#图像

c++编写函数实现图片缩小放大

在vc6.0中利用opencv显示图片,但自己写的函数来实现图像的放大缩小

c#里使显示图像picture一样大小

pictureBox 的 SizeMode 属性值改为 StretchImage,即可 说明: PictureBox.SizeMode 属性取值说明: AutoSize 调整 PictureBox 大小,使其等于所包含的图像大小。 CenterImage 如果 PictureBox 比图像大,则图像将居中显示。如果图像比 PictureBox 大,则图片将居于 Pictu

pudongdong的博客 4114

C#调用打印机,打印图片

转自http://blog.csdn.net/yuanhong55/article/details/14447969 PrintPreviewDialog是打印预览对话框,需要传一个 printDocument给它才可以显示该对话框 PrintDocument是具体要打印的内容,可以是图片,也可以是加载文档,文字。 代码如下: [cs

梁光成——专注IT技术的点点滴滴 1581

C#实现在PictureBox显示一张本地图片

效果 方法 新建一个Form,并在Form中添加pictureBox控件 在Form中初始化PictureBox: //常用设置图片缩放模式: //1.AutoSize 控件大小等于图片大小 //2.Zoom 控件大小不变,图片比例缩放后展示 pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; fileStream = new FileStream("图片路径", FileMode.Open, FileAccess.Read); pict

FarryNiu的博客 7112

C#点击事件缩放图片

// skinPictureBox16是否放大 public bool fullscreenSkinPictureBox16 = false; /// <summary> /// skinPictureBox16点击事件:点击后图片放大或缩小 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void

欢迎道友来访 926

图片根据窗口大小按比例缩放

带有Frame的页面: &lt;script language="JavaScript"&gt;    if(window.frameElement!=null)  {      window.frameElement.attachEvent("onresize",winResize);  }  else  {      window.attachEvent("onresize",winResi...

1369

c#实现图片缩放【实测成功】

仅作为记录,大佬请跳过。 参考:感谢大佬博主文章传送门 博主实现的源代码 根据大佬博主文章中修改了一个bug后,可运行的程序: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; usi

weixin_41529093的博客 8879
上一篇: 汉诺塔搬运模拟程序(C#.net 2003)
下一篇: 五子棋智能算法解析(VC .net)
zhyuanshan
博客等级 码龄20年 202粉丝 27原创
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值