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="";
}
ASP.NET前后端用户安全管理技术全解析 本文系统介绍ASP.NET Core中用户管理的核心技术,涵盖注册登录、密码加密(PBKDF2)、JWT身份认证及AccessToken/RefreshToken机制。通过轻量级Web API项目演示,强调安全实践:密码明文不存储,使用加盐哈希;登录后生成带有效期的JWT令牌,结合短时效AccessToken与安全存储的RefreshToken实现无状态身份验证,提升系统安全性与用户体验。 阅读详情

相关推荐

基于DeepSeek的保险续保预测:Transformer时序模型与特征工程实战

时序预测是客户流失预警的核心技术,通过分析行为序列数据提前识别风险。其原理基于Transformer编码器捕捉长期依赖,结合滑动窗口统计与相对位置编码提升预测精度。技术价值在于将被动提醒转为主动预判,降低获客成本并提升续保率。典型应用于保险、金融等客户生命周期管理场景。本文围绕DeepSeek续保方案,详解从多源数据接入、时序特征工程到Focal Loss不均衡处理的工程落地路径,并给出策略生成与避坑指南。

weixin_30745553的博客 310

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...

870

stb-translator-data-repository.zip - Web 版翻译者资料库项目源码

本项目是由 VB6/Qt 桌面版《翻译者资料库》移植而来的 Web 版本,采用 Spring Boot 3.3 + Thymeleaf + Bootstrap 5.3 + MySQL 8.0 技术栈,面向翻译与写作场景提供一站式资料管理底座。系统涵盖资料管家、分类词库、句库、文本处理器、中英对照制作、通用查找、数据备份恢复及旧库迁移等核心模块,完整继承桌面版的编号连续、引号规范化、查重、RTF 附件同步等关键业务规则。前端采用服务端渲染配合本地静态资源,支持离线使用;通过 run.bat 一键完成环境检查、建库、编译与启动,访问 http://localhost:8080 即可使用。

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编程 2402

利用LQR反馈控制器在不同风况下模拟风力涡轮转速控制的MATLAB.zip

1.版本:matlab2014a/2019b/2024b 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

基于jdk21、jdk17、jdk8 + SpringCloud + SpringBoot 开发的微服务中后台快速开发平台,专注于多租户、开放平台解决方案,亦可作为普通项目的基础开发框架使用

基于jdk21、jdk17、jdk8 + SpringCloud + SpringBoot 开发的微服务中后台快速开发平台,专注于多租户(SaaS架构)、开放平台解决方案,亦可作为普通项目(非SaaS架构)的基础开发框架使用,目前已实现数据库隔离、字段隔离等租户隔离方案。

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

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

weixin_39733805的博客 1349

高校技术转移办公室人员如何提升技术成果的推介效果和市场转化率?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

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的博客 824

两相交错并联BuckBoost变换器仿真模型(Simulink仿真实现)

内容概要:本文档系统整合了电力电子与新能源领域的Simulink仿真资源,聚焦于两相交错并联Buck-Boost变换器的双闭环PWM控制建模与仿真,同时涵盖多种典型拓扑结构,包括交错并联Boost、PFC功率因数校正、LLC谐振变换器、双向CLLC、DAB变换器、VIENNA整流器等。资源体系完整,包含仿真模型、设计报告、计算书、PPT说明文档及Matlab/Python代码,覆盖从基础建模到高级控制策略(如软开关技术、谐波抑制、状态估计、双闭环控制)的全流程。此外,还拓展至状态估计(卡尔曼滤波、粒子滤波)、智能优化算法、机器学习、信号处理、路径规划等多个交叉学科方向,构建了一个面向科研与工程应用的综合性技术平台。; 适合人群:电气工程、自动化、电力电子、新能源科学与工程等专业的硕士博士研究生、高校科研人员,以及从事新能源发电、智能电网、电动汽车、电力传动等领域技术研发的工程师。; 使用场景及目标:①开展电力电子变换器(如Buck-Boost、LLC、DAB、PFC等)的建模、控制与仿真研究;②支撑课程设计、学位论文、科研项目中的系统仿真与算法验证;③学习并复现高水平期刊(如EI)提出的先进控制策略,如基于KKT条件的最优控制、扩展移相调制、恒压恒流双环竞争控制等;④结合智能算法(如粒子群、秃鹰算法)进行微电网调度、储能配置、路径规划等多目标优化问题的研究与快速原型开发。; 阅读建议:建议依据提供的资源目录循序渐进地查阅资料,优先选取与自身研究方向高度契合的内容进行深入学习;应结合仿真模型与配套文档动手实践,调试参数并分析结果,充分利用百度网盘与公众号渠道获取完整资源,提升科研工作的系统性、规范性与创新性。

【基于Zernike矩的良性和恶性肿块的分类】应用于乳腺癌诊断中的快速相反权重学习规则(Matlab代码实现)

内容概要:本文详细介绍了基于Plecs软件构建的全桥LLC谐振变换器仿真模型,重点阐述了其在软开关技术条件下的建模方法与仿真分析过程。该模型能够精确模拟LLC谐振变换器在不同运行工况下的动态响应特性,涵盖开环与闭环控制策略的设计与实现,适用于研究变换器的效率优化、谐振参数匹配、系统稳定性及软开关性能等问题。相较于Simulink等传统仿真工具,Plecs在高频电力电子系统仿真中展现出更高的仿真速度与数值精度,尤其适合用于复杂电力电子拓扑的快速原型验证与优化设计。; 适合人群:具备扎实电力电子技术基础和仿真经验,从事高频功率变换器设计、新能源电源开发、电力电子系统研究的研发工程师及高校研究生。; 使用场景及目标:①深入理解全桥LLC谐振变换器的工作原理与软开关实现机制;②掌握在Plecs环境中搭建高精度、高效率电力电子系统仿真模型的方法;③开展变换器控制策略(如变频控制、闭环稳压)的仿真验证与参数优化;④为科研课题、学位论文或工业项目提供可靠的仿真平台与技术支持。; 阅读建议:建议结合相关电力电子教材与LLC变换器设计指南,深入掌握谐振参数计算与系统稳定性分析方法,并通过反复调试仿真模型,观察关键波形(如原边电流零点切换、副边整流波形),以全面理解软开关过程的物理本质与控制实现细节。

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

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

weixin_30273501的博客 363

产业园区运营负责人如何提升园区内技术成果与企业需求的精准对接效率?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

绘图类—Graphics

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

CARROTHWQ的专栏 1290

UVM概述及uvm_component和uvm_object(一)

UVM:Universal Verification Methodology(通用验证方法学) UVM:是建立在systemverilog平台上的一个库,提供了一系列的接口,让我们能够更方便的进行验证。验证平台组成: Driver:用来把不同的激励施加给DUT; Monitor:用来检测DUT的输出; Scoreboard:专门用来比较期望值与monitor检测到的DUT输出; Refer

南国之邱 1万+

提供一个微信投票刷票的js代码

最近几天,有个朋友找我去写一个脚本帮他 在微信上刷票、微信投票刷票,我大概看了一下,本次微信投票刷票其实就是根据微信的openid来限制,每个openid只能投三次票的限制。上图可以看出,其实只要openid,和toopenid即可,openid是自己的微信id,toopenid是投票对象的微信id,那我们定时修改openid即可给某一个人刷投票。把这段代码复制到浏览器F12之后的console里,直接回车即可开始微信投票刷票(刷新或关闭浏览器即可停止刷票)//生成随机的ip地址。

qq_858865388专业提供快速微信投票教程攻略和方法的博客 2万+

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的博客 180

谷歌账号国家或地区设置修改全攻略:条件、流程与避坑指南

谷歌账号的国家或地区设置与付款资料、账号历史行为及登录环境强绑定,修改时需满足特定条件。其原理在于地区作为索引键,关联内容库、货币结算和风控策略。正确切换可解决内容推荐错乱、付款失败等问题,适用于搬家、订阅区域限定服务等场景。操作前需在目标地区创建付款资料、清空余额并取消订阅,且一年仅能修改一次。本文详解从准备到验证的完整流程,并揭示官方文档未提及的实操陷阱,助你安全完成地区切换。

weixin_42521558的博客 2269

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

Opencv计算相机响应函数(Camera Response Function)与Radiance

本文主要用于记录自己在学习CRF过程中遇到的一些问题。 CRF模型 I=f(V(t⋅L))I=f(V( t\cdot L))I=f(V(t⋅L)) 其中III为相机灰度值Intensity,LLL为环境中irradiance,ttt为曝光时间,VVV为相机镜头参数,fff为相机响应函数CRF。 有些论文中认为相机镜头参数是线性的不考虑,但有些考虑了衰减即靠近图像边缘亮度会暗一些,但没有看到二者同时计算的,如果同时计算两个非线性的函数,我觉得可能会有不确定性,在这里还请教一下了解的朋友。 控制

larrydong的博客 6000

VS2022+Qt6保姆级配置指南:从插件安装到项目互通全流程(附常见错误解决)

本文提供了一份详细的Visual Studio 2022与Qt6集成开发环境配置指南。从插件安装、环境搭建到项目创建与互通,涵盖了全流程操作步骤,并针对常见的配置错误和调试问题提供了实用的解决方案,帮助开发者高效构建企业级桌面应用。

nginx7reverse的博客 1204
上一篇: 怎样使用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、付费专栏及课程。

余额充值