DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reduce Frontend Complexity in ASP.NET Razor Pages Using HTMX
  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel

Trending

  • The Middleware Gap in AI Agent Frameworks
  • Runtime Formula Evaluation With MVEL Library in Spring Boot
  • 11 Agentic Testing Tools to Know in 2026
  • AI-Driven RAG Systems: Practical Implementation With LangChain
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET MVC 3: Building simple image editor using WebImage helper

ASP.NET MVC 3: Building simple image editor using WebImage helper

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Nov. 09, 10 · News
Likes (0)
Comment
Save
Tweet
Share
16.5K Views

Join the DZone community and get the full member experience.

Join For Free

In my previous posting about WebImage helper I introduced how to use WebImage for different image manipulations. In this posting I will show you how to build simple online image editor using WebImage helper.

Source code

You can find source code of this example from Visual Studio 2010 experiments repository at GitHub.

Source code @ GitHub Source code repository
GitHub

Simple image editor belongs to Experiments.AspNetMvc3NewFeatures solution, project name is Experiments.AspNetMvc3NewFeatures.Aspx.

Here you can see the screenshot of simple image editor.

Simple WebImage editor

Four simple operations is enough for current example.

Building editor view

Before going to controller that is extremely simple – believe me, it is no-brainer – let’s take a look at view. The editor is made up from following parts:

  • image tag that shows image with current effects,
  • effects checkboxes,
  • JavaScript that requests image with selected effects from server.

Here is the view.

<%@ Page Title="Image Worksop" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Image Workshop
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function updateImage() {
var req = '/ImageWorkshop/GetImage?';
var query = '';

if ($('#HorizontalFlip').is(':checked'))
query += '&HorizontalFlip=1';
if ($('#VerticalFlip').is(':checked'))
query += '&VerticalFlip=1';
if ($('#RotateLeft').is(':checked'))
query += '&RotateLeft=1';
if ($('#RotateRight').is(':checked'))
query += '&RotateRight=1';

req += query.substr(1);
$('#image').attr("src", req);
}
</script>

<h2>Image Workshop</h2>

<% using(Html.BeginForm()) { %>
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td valign="top" rowspan="10">
<img src="/ImageWorkshop/GetImage" id="image" alt="" />
</td>
<td valign="middle"><input type="checkbox" id="HorizontalFlip" value="1" />
Flip horizontally</td>
</tr>
<tr>
<td valign="middle"><input type="checkbox" id="VerticalFlip" value="true" />
Flip vertically</td>
</tr>
<tr>
<td valign="middle"><input type="checkbox" id="RotateLeft" value="true" />
Rotate left</td>
</tr>
<tr>
<td valign="middle"><input type="checkbox" id="RotateRight" value="true" />
Rotate right</td>
</tr>
<tr>
<td><input type="button" value="Preview" onclick="updateImage()" /></td>
</tr>
</table>
<% } %>
</asp:Content>

The JavaScript function updateImage() checks what checkboxes are checked and creates query string for GetImage controller action. This query string contains values only for applied effects.

Controller actions

When you take a look at image definition in view you can see that we need two separate actions – one that returns editor view and the other that returns image. I said before that controller is extremely simple. Don’t mind my not so nice solution for image action – it is my working draft.

public class ImageWorkshopController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}

public void GetImage(string horizontalFlip="", string verticalFlip="",
string rotateLeft="", string rotateRight="")
{
var imagePath = Server.MapPath("~/images/bunny-peanuts.jpg");
var image = new WebImage(imagePath);

if (!string.IsNullOrWhiteSpace(verticalFlip))
image = image.FlipVertical();
if (!string.IsNullOrWhiteSpace(horizontalFlip))
image = image.FlipHorizontal();
if (!string.IsNullOrWhiteSpace(rotateLeft))
image = image.RotateLeft();
if (!string.IsNullOrWhiteSpace(rotateRight))
image = image.RotateRight();

image.Write();
}
}
Applying effects to image is very easy. We just check the values sent from view and apply the effect only if appropriate value is not null or empty string. That’s why I created query string with effect values as ones ("1") in JavaScript.
ASP.NET MVC ASP.NET

Published at DZone with permission of Gunnar Peipman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Reduce Frontend Complexity in ASP.NET Razor Pages Using HTMX
  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook