Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2004

Tip: Looking for answers? Try searching our database.

Image Resizing and Renaming...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Walter - 09 Mar 2004 15:55 GMT
I have the following code that loads an image, resizes it as a new
image and then saves to a new file, the original image file then gets
deleted and the new image is renamed to the old filename.

Now I get the following exception on the File.Delete line

System.IO.IOException: The process cannot access the file
"C:\test.jpg" because it is being used by another process. at
System.IO.__Error.WinIOError(Int32 errorCode, String str) at
System.IO.File.Delete(String path) at
tt360.Cms.Adm.Content.Objects.EditDimensions.Save_Click(Object sender,
EventArgs e) in c:\vss\tt3\cms\002\web\tt360.cms.adm\content\objects\editdimensions.aspx.cs:line
215

I read that System.Drawing.Image.FromFile would keep the file open so
I changed the code to use System.Drawing.Image.FromStream so I can
explicitly close the file but it appears this is not the case as the
exception occurs from the file not being closed.

Now I have another web page that also accesses the original file but
only to open it to generate a thumbnail. I use the same FromStream
method to open and then close the file. I am pretty sure this page is
not keep the file open either. Nothing else is accessing this page.

This code runs in a submit button click handler and it takes maybe 10
seconds of waiting/clicking the button before it saves correctly. Is
it this page that's not working correctly or could it be to do with
the other page?

Thanks in advance for your help.

Rob.

    string "C:\\test.jpg";
    string "C:\\new_test.jpg";

    GenerateThumbnail(fileName, newFileName);

    System.Drawing.Image image = null;
    FileStream fs = null;

    try
    {
        // image = System.Drawing.Image.FromFile(Context.Server.MapPath(fileName));
       
        fs = new FileStream(Context.Server.MapPath(fileName), FileMode.Open,
FileAccess.Read, FileShare.Read);

        image = System.Drawing.Image.FromStream(fs);
    }
    catch { }

    if (image != null)
    {
        int _maxWidth = 0;
        int _maxHeight = 0;

        try
        {
            _maxWidth = Convert.ToInt32(Width.Text);
        }
        catch { }

        try
        {
            _maxHeight = Convert.ToInt32(Height.Text);
        }
        catch { }

        System.Drawing.Bitmap thumbnailBitmap = new
System.Drawing.Bitmap(_maxWidth, _maxHeight);

        System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(thumbnailBitmap);

        graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0,
_maxWidth, _maxHeight));

        thumbnailBitmap.Save(Context.Server.MapPath(newFileName),
image.RawFormat);

        thumbnailBitmap.Dispose();
        thumbnailBitmap = null;

        image.Dispose();
        image = null;

        graphics.Dispose();
        graphics = null;
    }
    else
    {
        ResponseMessage.Text = "Could not load image. ";
    }

    if (fs != null)
    {
        fs.Close();
        fs = null;
    }

    try
    {
        File.Delete(Server.MapPath(fileName));
        File.Move(Server.MapPath(newFileName), Server.MapPath(fileName));

        ObjectImage.ImageUrl = fileName;

        EditPanel.Visible = false;
        ResponseMessage.Text = "The image has been updated. ";
    }
    catch (Exception ex)
    {
        ResponseMessage.Text = ex.ToString();
    }
Nicholas Paldino [.NET/C# MVP] - 09 Mar 2004 16:00 GMT
Robert,

   You might want to try loading the contents of the file into memory first
(a MemoryStream), and then passing that to the FromStream method.  This way,
you know the file is never opened.  However, if the files are particularly
large, it might have a performance impact (given that the contents of the
file have to be loaded completely into memory).

   Hope this helps.

Signature

              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard.caspershouse.com

> I have the following code that loads an image, resizes it as a new
> image and then saves to a new file, the original image file then gets
[quoted text clipped - 8 lines]
> tt360.Cms.Adm.Content.Objects.EditDimensions.Save_Click(Object sender,
> EventArgs e) in c:\vss\tt3\cms\002\web\tt360.cms.adm\content\objects\editdimensions.aspx.cs:
line
> 215
>
[quoted text clipped - 99 lines]
> ResponseMessage.Text = ex.ToString();
> }
Robert Walter - 10 Mar 2004 15:02 GMT
> Robert,
>
[quoted text clipped - 3 lines]
> large, it might have a performance impact (given that the contents of the
> file have to be loaded completely into memory).

What is the easiest way to load the file into a memory stream?

Thanks,

Rob.
Dave the Troll - 28 Oct 2004 14:53 GMT
I had the exact same problem with an application to rename image files based on the metadata stored in them.

Here is the final solution snippet

                System.IO.FileStream file = System.IO.File.OpenRead(path);
                byte[] all = new byte[file.Length];
                file.Read(all, 0, all.Length);
                file.Close();
                System.IO.MemoryStream mem = new System.IO.MemoryStream(all);
                Image img = Image.FromStream(mem);

---
Ahmed AbouTaleb - 10 Mar 2004 18:38 GMT
The best alias for this question is
microsoft.public.dotnet.framework.drawing.

Thanks

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.