摘至: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="data: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="data:imager.aspx"/>

Ex1: Adding two images

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

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="";
}

转载于:https://www.cnblogs.com/EasyData/archive/2007/10/04/914209.html

ASP.NET Image Manipulation Examples: Adding, Zooming, Enlarging相关推荐

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(36)-文章发布系统③-kindeditor使用...

    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(36)-文章发布系统③-kindeditor使用 原文:构建ASP.NET MVC4+EF5+EasyUI+Un ...

  2. 从底层了解ASP.NET体系结构

    A low-level Look at the ASP.NET Architecture Getting Low Level  进入底层: This article looks at how Web ...

  3. asp.net mvc Partial OutputCache 在SpaceBuilder中的应用实践

    最近给SpaceBuilder增加OutputCache 时发现了一些问题,贴在这做个备忘,也方便遇到类似问题的朋友查阅. 目前SpaceBuilder表现层使用是asp.net mvc v1.0,使 ...

  4. react 数据可视化_使用d3创建数据可视化并在2020年做出React

    react 数据可视化 Data visualisation and application technologies have evolved greatly over the past decad ...

  5. rspec 测试页面元素_如何使用共享示例使您的RSpec测试干燥

    rspec 测试页面元素 by Parth Modi 由Parth Modi 如何使用共享示例使您的RSpec测试干燥 (How to DRY out your RSpec Tests using S ...

  6. ECShop替换FCKeditor编辑器为KindEditor

    为什么80%的码农都做不了架构师?>>>    理论上可以替换包括百度ueditor在内的其他任何编辑器,步骤包括: 1.上传新编辑器代码 首先下载Kindeditor,解压后将文件 ...

  7. {%extends bootstrap/base.html%}的添加,使得其他block无法继承

    仙说{%extends "bootstrap/base.html"%}用法: 在base.html中调用一次即可,并且  {%extends "bootstrap/bas ...

  8. kindeditor项目集成

    2019独角兽企业重金招聘Python工程师标准>>> 本文介绍了kindeditor编辑器与java web进行集成,实现文章内容的上传,查询与修改,由于kindeditor图片默 ...

  9. 每周.NET前沿技术文章摘要(2017-05-10)

    汇总国内外.NET社区相关文章,覆盖.NET ,ASP.NET和Docker容器三个方面的内容: .NET Debugging .NET core with SOS everywhere 链接:htt ...

最新文章

  1. 详解Scala函数也是对象的特性
  2. java和c++的区别大吗_小自考本科含金量高吗?小自考和大自考有什么区别?
  3. python的进程线程和协程_python成长之路 :线程、进程和协程
  4. Bootstrap--导航栏样式编辑
  5. 天梯—打印沙漏以及剩余个数(C语言)
  6. Hbase权威指南(含目录,高清,免费)
  7. 无责任书评——DOOM启示录 BORLAND传奇
  8. java房屋出租系统
  9. 调和方程(拉普拉斯方程)基本解和边界元方法的积分计算
  10. PlaySound详解
  11. 重磅!Science发表西湖大学周强实验室关于“新冠”的最新研究成果
  12. 高纬度矩阵运算--NumPy
  13. UNI-APP 开发微信公众号(H5)JSSDK的使用、微信扫一扫
  14. 为什么SAST和SCA在SDLC中很重要?
  15. 自下而上和自上而下的注意力模型《Bottom-Up and Top-Down Attention for Image Captioning and Visual Question Answering》
  16. vue项目搭建(二)
  17. 2021年宝鸡中学高考成绩查询,宝鸡高中学校实力排名,2021年宝鸡所有的高中分数线排名...
  18. 使用手机可以在线拍照、翻译图片文字吗?
  19. 张一鸣宣布卸任字节跳动CEO
  20. AD自动布地孔和不需要阻焊层

热门文章

  1. Unity灯光(light)
  2. uni-app开发的h5,使用微信授权登录(前置条件+具体代码)
  3. 【VHDL】带使能端的同步复位的8位寄存器设计
  4. 最新酒桌小游戏喝酒小程序源码/带流量主
  5. linux qq x64,QQ Linux 版时隔多年再次更新,支持 x64、ARM64 等多种架构
  6. 哔哩哔哩2020校园招聘前端笔试卷(一)
  7. 简笔画花边边框超简单_花边简笔画简单又漂亮 手抄报的边框图片大全
  8. 国企的面试我们应该怎么准备?
  9. 在360新员工入职培训上的讲话
  10. HDR视频色调映射算法(之二:Adaptive temporal TMO)