ASP.NET Image Manipulation Examples: Adding, Zooming, Enlarging

C语言编写简单图像程序-圆 生成图像格式:PPM PPM图片格式介绍: A Netpbm format is any graphics format used and defined by the Netpbm project. The portable pixmap format (PPM), the portable graymap format (PGM) and the portable bitm 阅读详情

ASP.NET Image Manipulation Examples: Adding, Zooming, Enlarging

摘至:http://www.codeproject.com/aspnet/ASPImaging1.asp
Download source files - 32.3 Kb

Introduction

This is a sample project to do image manipulation in ASP.NET projects, using .NET's GDI library, and related classes.

The project demonstrates dynamically 'Adding two images', 'creating zoom effects', and 'enlarging images'.

Some notes on displaying dynamically generated images

If we have to generate an image dynamically and show it on a webpage, we have to do the image generation coding on a separate page and call it in our src="" attribute of the <img> tag.

<img src="imager.aspx"/>

This is the common and as-far-as I know, the only way to display dynamic images on a webpage, and not specific to the ASP.NET framework alone. It applies to whatever language and webserver you might use. And fortunately, this method is compliant with any client browser, since the process happens server-side.

Generating images

You would typically use all (or some) of the below .NET namespaces to generate / load preexisting images.

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

You can draw lines, arcs and do a lot of things using the Drawing namespace and the GDI/ GDI+ libraries... you should see articles on the same for doing that. In this article, we will only try manipulating pre-existing images.

The rest of this article will use classes and methods in the above mentioned namespaces.

Loading and Displaying an Image

To load and display an image, you would create a separate file such as imager.aspx and put no controls or anything on it... but just code for the Page_Load event as below:

private void Page_Load(object sender, System.EventArgs e)
{
Bitmap objImage = new Bitmap(strBasePath + file://images//fruity.jpg);
objImage.Save(Response.OutputStream,ImageFormat.Jpeg);
objImage.Dispose();
}

The above code loads an image from the images directory of the web project and pushes it to the OutputStream which is the response header that will be sent to the client.

Done that, we can put a call to the image as mentioned before:

<img src="imager.aspx"/>

Ex1: Adding two images

Sample screenshot

Having discussed displaying a single image .. it's very obvious now as to how to add two images and send to the client.

The simplest way being to push two images to the response stream. Or you can add the two images to a new image and push this image to the response stream as below:

Bitmap oCounter;
Graphics oGraphics;
oCounter = new Bitmap(23,15);
oGraphics = Graphics.FromImage(oCounter);
Bitmap objImage = new Bitmap(strBasePath + file://images//1.gif);
oGraphics.DrawImage(objImage,0,0);
objImage = new Bitmap(strBasePath + file://images//2.gif);
oGraphics.DrawImage(objImage,11,0);
oCounter.Save(Response.OutputStream,ImageFormat.Jpeg);
objImage.Dispose();
oCounter.Dispose();

Note: when drawing the second image, it's important to paste it at the proper position.. or else it overwrites or draws itself over the old image... this we do by manipulating the top left position of the drawimage() method, attributes... int X, and int Y.

If you are wondering why I named the final image object oCounter: typical application of adding two images would be generating 'hit counter' images for your clients.

Ex2: Zooming images

Sample screenshot

Sample screenshot

There are two ways of zooming an image.. or rather bringing out a zoom-effect on images.

  1. Enlarge the image to a larger size (this is discussed in the EX3).
  2. Copy a portion of the image and enlarge it to the size of the original image.

We discuss method (2) here.

In the sample project, I create the image using an ImageButton aspx control. This is to allow users to click on the image and pass x, y params to the server so that the clicked area of the image is zoomed (or technically enlarged).

The only function to learn to do this is:

Graphics.DrawImage()

DrawImage() accepts:

  • a source Rectangle (defining which portion is to be drawn),
  • a destination Rectangle (defining the target size and position of the image).

And if the destination rectangle is larger than the source rectangle, .NET automatically scales the image thereby getting our zooming effect.

Yes! This is important: "DrawImage() scales the image automatically if needed".

//get the portion of image(defined by sourceRect)
//and enlarge it to desRect size...gives a zoom effect.
////if image has high resolution.. effect will be good.
oGraphics.DrawImage(oItemp,desRect,sourceRect,GraphicsUnit.Pixel);

In the above code block, oItemp contains the image... the rest is clear I suppose.

Zoom - effect Logic

I think I should explain the logic I have used in my code (downloadable with this article - see top of article for download link).

What I do is I handle the image button click event, and get the x, y position where the user clicked on the image. I then do some basic calculation and mark my source rectangle around this click point...my source rectangle is 60% the size of the actual image itself. Since we discussed that source rectangle should be somewhere smaller than the destination rectangle for the scaling to happen. May be you can have a 50% size also, in which case the image gets zoomed more...

float iPortionWidth=(0.60f*oI.Width);
float iPortionHeight=(0.60f*oI.Height);
//oI is the image.

Multiple zooms..

I allow the user to click on the enlarged image also.. that means zoom it further and further. I achieved this with the below logic:

I have two hidden fields on the form.. where I store the user clicked positions like:

xpos = 100 ypos = 200

On the second click, the hidden fields take the values:

xpos = 100,322 ypos = 200, 123

and so on.. so I have a track of all the clicks the user made. I then split this string of values and perform the zoom operation on the original image multiple times.

That means, if user clicked at 100,200 first time and on the zoomed image he clicked at 322,123, then in the second postback of the form, I scale the image twice using a for loop every time at the respective points.

Hope that sounds clear. Anyways, the code is also well-documented.. and you will see it work as you break-into the code.

EX3: Enlarging the image (zoom-effect 2)

Auto-scaling doesn't alone happen with the DrawImage() method, it also can happen when you load the image.

Steps:
  1. load an image to actual size
  2. create an Image object and load the object (from prev step), with a width and height
Bitmap oImg, oImgTemp;
oImgTemp = new Bitmap(strBasePath + file://images//fruity.jpg);
oImg = new Bitmap(oImgTemp,600,400);

That's it.. the result is an enlarged image.

Observation:

I worked through this example creating all bitmap objects from the class Bitmap.

But when I tried the zoom-effect Ex:2, the Bitmap object won't scale to a new size whatever I do, .. but good fortunes, auto-scaling happened with the Image object.

The problem was with the DrawImage() method which would not do auto-scaling for bitmap image objects (supplied as the first argument)..

Hence, the below wont work.

//oBitmap is an instance of class 'Bitmap'
oGraphics.DrawImage(oBitmap,desRect,sourceRect,GraphicsUnit.Pixel);

While, this will...

//oItemp is an instance of class 'Image'
oGraphics.DrawImage(oItemp,desRect,sourceRect,GraphicsUnit.Pixel);

Change History:

Change 1:

Suppose you zoom the image (in EX2) once or twice, and then click button 'Original' and then again zoom once.. you will see the wrong result. It would have zoomed once + no. of times zoomed earlier before pressing 'original'. The problem is I am not clearing the hidden fields when 'original' button is pressed.

I forgot this.. really. :o)

I have now updated the download on this page (as on 8/Apr/04).. If u don't have the new download.. the only correction is:

//in index.aspx.cs page
private void Button1_Click(object sender, System.EventArgs e)
{
ImageButton1.ImageUrl="imager.aspx";
txtPosX.Value="";
txtPosY.Value="";
}
基于DeepSeek的保险续保预测:Transformer时序模型与特征工程实战 时序预测是客户流失预警的核心技术,通过分析行为序列数据提前识别风险。其原理基于Transformer编码器捕捉长期依赖,结合滑动窗口统计与相对位置编码提升预测精度。技术价值在于将被动提醒转为主动预判,降低获客成本并提升续保率。典型应用于保险、金融等客户生命周期管理场景。本文围绕DeepSeek续保方案,详解从多源数据接入、时序特征工程到Focal Loss不均衡处理的工程落地路径,并给出策略生成与避坑指南。 阅读详情

相关推荐

春秋BC492年世界历史地图矢量文件SHP格式.zip

本资源为《春秋BC492年世界历史地图矢量文件SHP格式》压缩包,文件大小约 2.26 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

svg图动态添加svg节点_如何在WordPress中添加SVG(带有2个简单解决方案)

svg图动态添加svg节点Do you want to add SVG files to your WordPress site? By default, WordPress allows you to upload all popular image, audio, and video file formats, but SVG is not among them. In this articl...

871

ComfyUI/MOSS Image 翠鸟线稿控制插画

文件编号:c0263 输入翠鸟线稿和颜色描述,生成线稿控制的珐琅风插画。样例按原画保留鸟、枝叶与鸢尾花的位置,并分配深青、钴蓝和琥珀配色。 解压后将 c0263.json 导入 RunningHub 工作台。若在本地 ComfyUI 使用,请按已安装的节点和模型版本调整工作流。

Accurately Profiling Direct3D API Calls (Direct3D 9)

Accurately Profiling Direct3D Is DifficultHow to Accurately Profile a Direct3D Render SequenceProfiling Direct3D State ChangesSummaryAppendix Once you have a functional Microsoft Direct3D application

痞子龙3D编程 2403

ComfyUI/Geeky Audio Mixer 讲解双轨混音

文件编号:c0254 输入两条音轨并分别设置音量,合成一条讲解配乐 MP3。实测把约 7.90 秒的讲解混入 10 秒配乐,背景音量设为 0.42 倍,输出保留 10 秒总长度。 解压后将 c0254.json 导入 RunningHub 工作台。若在本地 ComfyUI 使用,请按已安装的节点和模型版本调整工作流。

春秋BC755年世界历史地图矢量文件SHP格式.zip

本资源为《春秋BC755年世界历史地图矢量文件SHP格式》压缩包,文件大小约 26.59 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

GAMES101笔记_Lec05~06_光栅化 Rasterization

1 视口变换-从裁剪空间到屏幕空间 Canonical Cube to Screen 1.1 什么是屏幕 What is a screen? 在图形学中,抽象的认为屏幕是一个二维数组,数组中每一个元素是一个像素 An array of pixels 这个数组的大小:分辨率 Size of the array: resolution 一种典型的光栅成像设备 A typical kind of raster display Raster == screen in German Rasterize == d

Ziiur的博客 825

全国统一大市场反垄断大模型智能审查平台规划方案.pptx

全国统一大市场反垄断大模型智能审查平台规划方案.pptx

绘图类—Graphics

通过前面几章的学习,我们基本上解决了命令行模式的程序设计问题。现在开始要转向图形模式的程序。Java提供的Graphics类是用于绘图和显示格式化文字的工具。绘图必须在一个窗口(容器)中进行,Java小程序Applet就是一个窗口。本章我们只介绍Graphics类的绘制基本图形和格式化文字显示这两部分功能,其余内容在以后需要的时候再研究。 12.1 在Applet中写字和画图 要创建一个App

CARROTHWQ的专栏 1290

基于RRT与贪心平滑的PUMA560机械臂避障+路径规划及运动学仿真(改进前后对比)

内容概要:本文系统梳理了多个基于Matlab和Simulink的工程技术仿真实现案例,涵盖自动化控制、智能优化、能源系统、路径规划、信号处理及电力电子等多个前沿领域。重点内容包括:基于RRT与贪心平滑算法的PUMA560机械臂避障与路径规划仿真,展示了传统路径规划算法的改进与性能对比;永磁同步电机的非奇异快速终端滑模控制与无传感器速度控制策略研究,体现先进控制理论在电机驱动中的应用;多AUV协同路径跟踪中事件触发通信机制的仿真复现,验证其在稀疏通信条件下的协调性能与鲁棒性;此外还包括分数阶系统Lyapunov指数谱分析、悬臂梁有限元建模与多重网格求解、混合储能系统优化配置、多无人机协同运输与三维路径规划等综合性课题,充分展现了现代工程仿真在复杂系统建模与智能算法融合方面的深度与广度。; 适合人群:具备一定编程基础和工程背景,从事自动化、电气工程、机器人、控制科学、能源系统等领域的科研人员及工作1-3年的研发工程师。; 使用场景及目标:①掌握RRT、贪心平滑、滑模控制、事件触发通信等经典与前沿算法的实现原理与仿真技巧;②学习如何将智能优化算法(如麻雀优化、蜣螂算法、粒子群算法)应用于路径规划、调度优化等实际工程问题;③理解多智能体系统、非线性控制系统、有限元分析等复杂系统的建模与仿真方法。; 阅读建议:建议结合文中提供的Matlab/Simulink代码进行实践操作,重点关注算法设计逻辑与仿真模型构建过程,通过对比改进前后的仿真结果加深对算法性能的理解,并可进一步拓展应用于自身研究课题中。

基于多目标遗传NSGA-II算法的水火光系统多目标优化调度研究(Matlab代码实现)

基于多目标遗传NSGA-II算法的水火光系统多目标优化调度研究(Matlab代码实现)内容概要:本文研究了基于多目标遗传NSGA-II算法的水火光系统多目标优化调度问题,并提供了完整的Matlab代码实现。研究综合考虑了水电、火电与光伏发电的互补特性,构建了包含经济性、环保性和系统稳定性等多重目标的优化模型。通过NSGA-II算法求解,获得一组Pareto最优解集,有效解决了多能源系统中复杂的调度冲突问题。文中详细阐述了模型构建过程、算法实现细节及仿真结果分析,展示了该方法在降低运行成本、减少碳排放和支持可再生能源消纳方面的优越性能。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员以及从事能源系统优化调度相关工作的工程技术人员。; 使用场景及目标:①应用于多能源耦合电力系统的优化调度研究,实现经济效益与环境保护的平衡;②为掌握NSGA-II算法在复杂工程问题中的实际应用提供学习范例;③支撑学术论文复现、课题研究或工程项目方案设计。; 阅读建议:建议读者结合文中提供的Matlab代码进行逐模块调试与运行,深入理解算法流程与模型构建逻辑,同时可尝试修改目标函数或约束条件以适应不同的应用场景,进一步提升实践与创新能力。

ASP.NET中关于路径(Path)的相关知识(转载)

也许你曾遇到过这样的情况 运行类似的代码 StreamReadersr=newStreamReader("Test.txt"); 运行时会提示:"Could not find file 'C:"windows"system32"Test.txt". 这就是路径不对造成的 幸好在asp.net的request对象中有许多与路径相关的属性 ...

weixin_30273501的博客 363

手把手教你用Microsoft AEC Challenge数据集训练自己的回声消除模型(附完整代码)

本文详细介绍了如何使用Microsoft AEC Challenge数据集训练专业的回声消除模型,包括环境准备、数据清洗、模型架构设计、训练流程和实际部署注意事项。通过完整的代码示例和实战指南,帮助开发者快速掌握AEC技术,提升语音通信系统的回声消除效果。

weixin_27697385的博客 463

java如何在面板上实现线程,使用线程在Java中绘制面板

I am writing a program that has a number of different views. One of which is fairly graphics intensive (it displays an interconnected graph). Others just display small but complex diagrams.I'm finding...

weixin_29523709的博客 181

龙虾 Skill 技能库|OpenClaw+Hermes 全集成 一键调用所有 AI 技能

龙虾 Skill 技能库、OpenClaw、Hermes Agent、AI 技能集成、本地 AI 自动化、办公工具、数字员工

weixin_42219287的博客 628

电流镜结构实战:从零理解MOS管偏置电路设计(附吴金课程补充)

本文深入解析电流镜结构在MOS管偏置电路设计中的关键作用,从基本原理到实战应用全面覆盖。通过分析偏置电路的核心原理、破解设计难题的实用技巧,以及进阶优化策略,帮助工程师掌握这一模拟电路设计的核心技术。特别适合需要理解电流镜结构和基准电路设计的读者参考学习。

ujm5678901的博客 267

Chapter 10 - Surface Shading (fundamentals-of-computer-graphics-4th)

To make objects appear to have more volume, it can help to use shading, i.e., the surface is “painted” with light. This chapter presents the most common heuristic shading methods. 为了使物体看起来有更多的面,我们可以使用阴影。即:表面是用光“绘制”的。本章介绍最常见的启发式着色方法。 Diffuse Shading 世界上许多.

Yannie's Blog 461

开放平台 - 互动玩法演进之路

随着直播业务和用户规模日益壮大,如何丰富直播间内容、增强直播间内用户互动效果,提升营收数据变得更加关键。

bilibili_TC的博客 1761

path manipulation怎么解决_PyCharm报错与解决方法一览

报错:IndentationError:expected an indented block分析:缩进错误!解决:你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。往往有的人会疑问:我根本就没缩进怎么还是错,不对,该缩进的地方就要缩进,不缩进反而会出错报错:AttributeError: 'NoneType' object has no attribute 'shape'分析:多发...

weixin_39733805的博客 1349

大数据面试 逻辑题

逻辑题大致分为比较逻辑题、分析逻辑题、综合逻辑题、抽象逻辑题、概括逻辑题、推理逻辑题、论证逻辑题等,由于这类题比较考验综合素质,因此也一直是各大企业笔试、面试时经常喜欢考察的题目类型之一。很多童鞋认为逻辑题很难,其实不然。多做练习,打开思路,这类题还是可以轻易化解的。 下面为大家整理了网络上最全的75道逻辑判断测试题。在每道题目下方有答案,是用白色显示的,反选就可以看到啦~ 【1】假设有一个池...

zzq_0916s的博客 4426

星网宇达Newton-M2设备安装及配置

主要介绍了星网宇达Newton-M2设备的配置过程

weixin_49353445的博客 2689

NCCL-AllReduce-P2P 与 NET(GDR)数据传输过程分析 (八)

四个 rank 执行 AllReduce 求和时,Ring 和 Tree 采用不同的归约顺序。在 Ring 中,每个 rank 从前驱接收数据,向后继发送数据。每个 chunk 沿环逐跳归约,再沿同一方向分发归约结果。在 Tree 中,叶节点向父节点发送本地输入数据。中间节点将子节点发来的数据与本地输入数据逐元素相加,再把部分和发给父节点。根节点完成归约后向下广播结果。两种算法都沿当前 Channel 的邻接关系收发。节点内走 P2P 时,GPU 通过映射地址,经 PCIe 或 NVLink 访问对端显存。

非专业业余程序员 357

让 ME51N 在采购信息记录过期时不要带出上一次采购订单的价格

在采购信息记录(PIR)价格条件无效或过期时,到上一次采购订单价格进行提议;你可以通过关闭,并在把PO price设置为不采纳,再辅以把过期的价格来源直接拦截为错误。若仍需 100% 阻断,可用在保存前清空估价价格作为技术兜底。标准行为与配置证据见文内引用。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 680
上一篇: 怎样使用CHttpFile读取Unicode编码的网页
下一篇: Windows Form(.NET,C#)程序使用MFC控件(treeview)DLL,treeview上面的上下左右键不工作
sinsel
博客等级 码龄20年 3粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址