.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

C# Tutorial - Image Editing: Saving, Cropping, and Resizing

Posted By: Syed Shakeer Hussain     Posted Date: October 05, 2010    Points: 2   Category :ASP.Net
In C# it can be tiresome to do certain image editing functions using GDI+. This post has some fun editing methods which can come in handy at times. I have also included a nice little C# program to show all the functionality of the methods below.

Saving a Jpeg
The first thing to do here is set up the method signature with the input parameters. These are the save file path (string), the Image to save (System.Drawing.Bitmap), and a quality setting (long).

View Complete Post

More Related Resource Links

image.save not saving images


I have a piece of code that writes string into an image and save it. It works on one of my site and but not the other. The image.save is not working. If this is a permission problem, which account should I change? I use win server 2003 and IIS 6, and have given ASPNET, NETWORK SERVICE, and anonymous user write access.

OutOfMemory when resizing image


I've been pulling my hair out on this one. I have a image gallery module in asp.net. I'm using Telerik RadUpload to upload a sinlge image. I then save the image on the server and create a thumbnail and save it as well. There is no loop involved and is one at a time. It always seems to work when I run it locally on Windows 7 (IIS7) but on the server IIS6 I keep getting OutOfMemory errors. The images I'm trying are barely 1MB so not huge files. I've gone through google and bing over past several days and can't seem to find a solution. In my resize methods I'm using the "using" statement on the image reference, etc. I can pretty much always make it fail with the 3rd line below:

ruPhoto.UploadedFiles[0].SaveAs(Server.MapPath("~/DesktopModules/Gallery2/Photos/" + newPhotoName));
System.Drawing.Image photo = System.Drawing.Bitmap.FromFile(Server.MapPath("~/DesktopModules/Gallery2/Photos/" + newPhotoName));
//System.Drawing.Image img = new System.Drawing.Bitmap(photo); <--- will cause outofmemory error

if I just use the photo variable then the OutOfMemory will occur later in the resize code.

Any help would be greatly appreciated.

Editing Image in Gridview VB.net

Hi, i'm back again and would like to seek help and advises from you experts on how to deal with problem. I have a table that accepts news data like title, content, link and a single image to be uploaded and attached to the database (not on file server). Now i want this Image Column (varbinary datatype) to be edited in case. When I tried puting an html fileupload in editing mode using gridview, I am getting error from the Image Column conversion like Image type cannot be converted into string. I know there is this scenario of reading and assigning data in my table, and then changing some codebehind and at the same time binding it into the gridview.This is my Image Handler:<%@ WebHandler Language="VB" %>Imports SystemImports System.WebImports System.ConfigurationImports System.Data.SqlClientPublic Class Handler : Implements IHttpHandler        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest        Dim con As New SqlConnection()        con.ConnectionString = ConfigurationManager.ConnectionStrings("automotiveConnectionString").ConnectionString                ' Create SQL Command           &n

Saving Image File in a Folder on Sever from Client Machine using Sql Server

  Hi EveryOne... Can anyone help me Saving an Image file in a Server folder from a client machine using Sql server 2005....   

WPF Image editing

I've been beating my head against a problem for a couple of weeks.  I need to be able to copy a portion of a photo into a separate photo that will be "floater" over the original area.  I need to be able to move and rotate that floater (possible scale at some point), and, when I've gotten my desired look, recombine that area with the original. I've been able to get a rectangular region copied into a separate image control (I would like to be able to do a multiline polygon eventually).  I can them use TransformGroup to RenderTransform the smaller image.  The problem I'm having is getting the finished product to re-combine with the source bitmap.  The canvas doesn't change with the virtual changes, and, after a rotation, the floater image isn't truly a rectangle since the y values are different on the top,left and top, right. I have the Leadtools classes at my disposal for the CombineFastCommand, but I would need to be able to get a rectangular region into a bitmap that includes my RenderTransform's. Thanks, N. WhiteThanks, Noah

saving image in databse

how to store image files in sqlserver database i am using datatype  image  and also i tried with varbinary .but while placing image file as pc.jpg it showing   error

Auto-generating Name When Saving an Image

hi guys, I have a toolbar on my chart:  <Toolbar Enabled="True"> <Items> <dcwc:CommandUIItem CommandName="PrintPreview" /> <dcwc:CommandUIItem CommandName="Print" /> <dcwc:CommandUIItem CommandName="Separator"/> <dcwc:CommandUIItem CommandName="SaveImage" /> <dcwc:CommandUIItem CommandName="Separator" /> <dcwc:CommandUIItem CommandName="UserDefined_1" /> </Items> </Toolbar>     For the SaveImage, I want to be able to set my image name with its own name. the name can be found using:   string fileName = Request.PhysicalApplicationPath + @"graphs\"+User.Identity.Name+graphID+".jpg"; Right now, when I click save, it always show chart.png And also, I want to download the chart as jpg.   thanks guys.

Image Editing Components for ASP.NET


Our company is trying to find a component (ActiveX or Silverlight) that allows a seamless style of image editing through the web.

The features we need are brightness/contrast/resize/zoom/crop etc... The basic set of editing functions.

I'm having a really hard time finding a component that would support a server side style of editing.

The images being edited are stored as part of our system, and they are stored privately in a dedicated storage location tied to our web server. The images are not available publically as a .jpg file but can be accessed through an aspx page which streams the binary.

We'd need some way of having this control point to an aspx page as the source (the aspx page streams the image as binary) and allow the users to edit the image, without having to go through the whole File-Open-select image from the client.

And of course after performing the edits, we'd need to be able to save the image or send the binary back to the server.

Does anybody know of any components out there with this type of functionality?

resizing image after jcrop


Hi guys,

I'm using JCrop, by using Mikes great article at: http://www.mikesdotnetting.com/Article/95/Upload-and-Crop-Images-with-jQuery-JCrop-and-ASP.NET

There are two things I haven't been able to figure out how to do though:

(1) How do I check if the uploaded image is big enough? (i.e. how do I determine width of image is > 200px")

(2) How do I resize the cropped image to 200px in width.

Any help/ideas would be very much appreciated.



Saving asp panel or div as image in asp.net/c#


Hi Folks, 

  I am having a requirement and i believe most of you  have already did it. This is the problem, i am having a div with image controls and i want to save it as image[png or jpeg] in database on button click. I have googled a lot but couldn't find a good start.  

<div id="frame" title="xxx" class="resize" style="width:634px; height:410px; overflow:hidden;">
<img id="drag56800" runat="server" src="Images/circle_street.png" style="height:140px;width:140px;border-width:0px;left:773px;top:123px;position:fixed;" /><img id="drag54101" runat="server" src="Images/animals/horse.png" style="height:30px;width:30px;border-width:0px;left:800px;top:171px;position:fixed;" />
Any code ,suggestion or aricle will help me.


Problem with Editing image inside DetailsView inside UpdatePanel



I have a problem and I really hope you'll be able to help me.

I'm trying to add and edit an image which is located in a detailsview which is inside an UpdatePanel. After I read relevant topics in the forum I installed the Ajax Control Toolkit. Below is my source code. When I used it only for insert new image it worked great. The problem caused when I tried to implement it on the Edit mode, i.e. when I tried to edit the image and upload a different image.  Unfortunately it didn't work as I hoped. Nothing happend and the image hasn't been changed. I also tried to change the IDs (AsyncFileUpload ID) to be the same in both places but it didn't work either.

The source code:

<asp:DetailsView ID="dvMovie" runat="server" Height="50px" Width="695px"
                    AutoGenerateRows="False" DataSourceID="sdsMovieById"      
                     BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"

C# Snippet Tutorial - How to Draw Text on an Image

In this snippet tutorial we're going to be adding on to our series of C# image editing tutorials. This tutorial is going to provide the code required to do basic text drawing including anti-aliasing and text alignment.

For more information on C# image editing, check out our previous two tutorials: C# Tutorial - Image Editing: Saving, Cropping, and Resizing and C# Tutorial - Image Editing: Rotate.

Ok, let's get started. The first thing we need to do is get a Bitmap object from an image file.

Bitmap myBitmap = new Bitmap("C:\\myImage.jpg");

Now that we have a Bitmap object we can get a Graphics object from it. The Graphics object will be doing all of our text drawing.

Drawing and saving a vector image (emf/wmf or anything MSWord/PPT can read)


Hi! I've been tasked with generating graphs of data as images for the purpose of importing into MS Word documents and Powerpoint presentations.

I've done it using GIF images, but I'd really like to use a vector format, so they can be scaled in the document without losing quality, and of course keeping the document size down as there's lots of them.

MS Word and Powerpoint can import EMF, WMF and CGM formats, but I have no idea how to generate those in .NET.

Does anyone have some pointers for me to get started creating say EMF files from .NET?  I've looked all over the net and can't find much at all about it... I've gathered it's a job for GDI+, but can't find sample code or anything for .NET.

Many thanks!

Having trouble cropping an image (w3wp.exe has the file locked).


Hey Guys,

Hoping someone can help im having a real issue with resaving an image (after I get the crop area from a jquery control). I get a GDI error and when I run handle.exe on the file it is open with the w3wp.exe.

I have 2 methods (1 that saves images when they have been uploaded and one that is meant to open and resave the image with new dimensions [for cropping]).

        protected void btnCrop_Click(object sender, EventArgs e)
            string profilePicFilePath = Server.MapPath("~/AppWide/User/Profile_Pics/" + userForPage.ProfilePicFilePath + "_temp" + ".jpg");

            //1) Load up image into memory
            Bitmap tempImage = new Bitmap(profilePicFilePath);

            Rectangle r = new Rectangle(Int32.Parse(x.Value), Int32.Parse(y.Value), Int32.Parse(w.Value), Int32.Parse(h.Value));

            System.Drawing.Image permImageToSave = cropImage(tempImage, r);

            //2) Resize image in memory using co-ordinates from boxes

            permImageToSave.Save(profilePicFilePath, ImageFormat.Jpeg);

            //3) Save the new image over the top of the old image

        //only allows uploading of .bmp, .jpg and .gif
        protected void ccMediaUploader_FileUploaded(object sender, UploaderEventArgs args)

            //first get th

Capturing Signature on touch screen handheld device and saving the Signature Image as .jpg or .gif o


Hii every one

Iam in a requirement of capturing Signature on touch screen hand held device and saving the Signature Image as an .jpg or .bmp or .gif on the device.
Could someone give code for the above requirement.

Resize image and get new byte size without saving it


Hi Guys,

I'm making an application that needs to be able to take a user specified image file and upload it into a database that only accepts images of 10 KB or less. So I want to let the user select an image and then have the option of resizing it and if possible also decreasing the quality - I'm sure I can figure out how to do both of these things on my own but the thing I'm not sure about is how I can tell what size the resulting image will be without actually writing it to disk. I thought the SourceStream property of the BitmapSource class would be useful but when I try to access it I just get told that the stream has been closed. Any ideas?




My website: www.cjwdev.co.uk My blog: cjwdev.wordpress.com

Saving Gray8 with BmpBitmapEncoder results in 256 color palette and 16 color image. Why?


Saving a Gray8 image using the BmpBitmapEncoder doesn't give an error, but does multilate the image.  The header and color table are those of an Indexed8 gray scale image, but the pixels have only 16 possible values 00, 11, 22, ..., DD, EE, FF.  Is this by design?  A FormatConvertedBitmap from Gray8 to Indexed8 and saved using the BmpBitmapEncoder results in the correct image.  This code demonstrates:

    Dim R As New Int32Rect(0, 0, 1024, 768)
Dim B(R.Height - 1, R.Width - 1) As Byte
    For Y As Integer = 0 To R.Height - 1
For X 

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend