3

I have an asp.net 2010 project. I write a jpg to the filesystem. Then I display it in an Image control. Then I use this code on button click to allow the user to rotate it 90 degrees.

string path = Server.MapPath(Image1.ImageUrl) ;

// creating image from the image url
System.Drawing.Image i = System.Drawing.Image.FromFile(path);

// rotate Image 90' Degree
i.RotateFlip(RotateFlipType.Rotate90FlipXY);

// save it to its actual path
i.Save(path);

// release Image File
i.Dispose();

It does rotate (I can actually watch that happen in Windows Explorer). But when I run the app again and it grabs the file from its path, it still displays it in its original form.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
NutBar
  • 31
  • 1
  • 2
  • 1
    Hit F5, maybe the browser is pulling from the CACHE. – Saif Khan Feb 10 '11 at 19:56
  • Similar: http://stackoverflow.com/questions/2352804/how-do-i-prevent-clipping-when-rotating-an-image-in-c – Daniel A. White Feb 10 '11 at 19:57
  • Actually, hitting F5 DOES fix it. How can I fix this programmatically though? I tried to redirec to the same page to force the refresh but it didn't work. – NutBar Feb 10 '11 at 20:37
  • You need to disable caching of this. We don't have enough context of how you are serving up the image to tell you how to do that though... – Robert Levy Feb 10 '11 at 20:42
  • If I hit F5 just once, then it works no matter how many times I click the button. At the top of the page, on Load on have this code which I thought was disabling cashing... Page.Response.AppendHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0") – NutBar Feb 10 '11 at 20:48
  • 1
    that sets the cache control for the page, not the images contained within it – Robert Levy Feb 10 '11 at 21:33
  • 1
    I'd like to suggest a title change: "How do I get around browser caching when dynamically changing image file contents?" You already know how to rotate the image; what you want is how to get the browser to pick up on the change immediately. – TLS Feb 24 '11 at 19:42

2 Answers2

0

Try adding a dummy querystring to the end of the image like "image1.jpv?v=1". This works to prevent caching for things like Javascript files and it may do the same for you.

NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
0

This is the classic issue with browser-cached resource files. Alison's answer is one option. Another option is to change the name of the physical file. If you "version" the files, then after each change, the file name will be different. This does mean that you'll have to dynamically reference the image path so that you display the correct version, and you'll need some way to determine the file name for the current version. It does prevent the browser from displaying the previously cached file immediately after a change, though, because it's technically a new file to the browser at that point.

If you are using some sort of data source (XML files or database) to store metadata for the images, then you can add a "Version" column and store a simple integer. With each change, increment the integer value and use the new value in the file name. You can even extend this by saving the previous versions and allowing your users to "undo" actions by copying an older version of the file into a new version. This might require a more robust metadata storage implementation, though.

TLS
  • 3,090
  • 2
  • 25
  • 33