Adding an image to a (Blog)Entry in Community?



I'm struggling with adding an image to a Entry object and hope that you can help me out with this. The code I'm trying to use looks like this:

Image entryImage = new Image(imageName, imageName, postedFile.InputStream, blogEntry.ImageGallery, PublishState.Published, CurrentUser);
entryImage = ImageGalleryHandler.AddImage(entryImage);
where blogEntry is an Entry object that has just been created. When I run this code I get the following "strange" exception since the same file that I'm trying to upload works just fine when I upload it as for instance a profile picture:

"Image file could not be read, this could be because the file is corrupt or in an unsupported format."

Does anyone have a suggestion to what I'm doing wrong?

Best regards


Jan 22, 2010 17:01

Yes, I agree that it is pretty strange, especially as it works in one scenario but not in another.

What happens in AddImage is that the provided stream is written to a file, then opened using System.Drawing.Image.FromFile(...).

Do you have a stack trace and any additional information? Is the supplied stream in different states in the two scenarios or anything like that?


The reason why this "strange" exception is thrown is however because of a small deficiency in the .NET framework:

System.Drawing.Image.FromFile(path) throws OutOfMemoryException on basically any error it encounters, which almost always (except in the rare case where you truly are out of memory) is both inaccurate and even more confusing.

                return System.Drawing.Image.FromFile(path);
            catch (OutOfMemoryException ex)
                throw new FrameworkException("Image file could not be read, this could be because the file is corrupt or in an unsupported format.");
Jan 25, 2010 11:52

Hello Håkan and thank you for your reply.

Here is the stack trace that appears together with this exception:

[FrameworkException: Image file could not be read, this could be because the file is corrupt or in an unsupported format.]

EPiServer.Community.ImageGallery.Data.ImageGalleryFactory.OpenImageFromFile(String path) +81

EPiServer.Community.ImageGallery.Data.ImageGalleryFactory.AddImage(Image image) +1834

EPiServer.Community.ImageGallery.ImageGalleryHandler.AddImage(Image image, Boolean ignoreQuota) +87

EPiServer.Community.ImageGallery.ImageGalleryHandler.AddImage(Image image) +7

XXX.YYY.Templates.ZZZ.QQQ.CreateTestimonial.PublishTestimonial_Click(Object sender, EventArgs e) in [FilePath]:81

System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111

System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10

System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13

System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36

System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

It works fine if I point out an image located on my machine, like this:

FileStream stream = new FileStream(@"D:\test.jpg", FileMode.Open);
Image entryImage = new Image(imageName, imageName, stream, blogEntry.ImageGallery, PublishState.Published, CurrentUser);

I've also tried with a MemoryStream instead of the HttpInputStream but that doesn't work either.

Do you know if something like this is being done somewhere in the Relate+ templates that I can look at? Is it possible for you to see if you can reproduce the problem?

Best regards

Edited, Jan 25, 2010 15:14

I have not been able to reproduce the problem you are experiencing. I copied your code snippet from the original post and added some code to set everything up, but for me it works.


            Blog blog = new EPiServer.Community.Blog.Blog(Guid.NewGuid().ToString());
            blog = BlogHandler.AddBlog(blog);
            Entry blogEntry = new Entry(blog, null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
            blogEntry = BlogHandler.AddEntry(blogEntry);

            System.IO.FileStream fs = System.IO.File.OpenRead("Image.jpg");

            string imageName = Guid.NewGuid().ToString();
            Image entryImage = new Image(imageName, imageName, fs, blogEntry.ImageGallery, PublishState.Published, user);
            entryImage = ImageGalleryHandler.AddImage(entryImage);



Which Community version are you using? I did my tests using the current 3.2 SP1 codebase (ie, 3.2 SP1 hotfix 2).

Jan 26, 2010 9:17

Hello again Håkan,

Yes as you can read in my latest post I also get it to work using a locally stored image and a FileStream, but when using a FileUpload control I can't get it to work, if it's possible for you to try this for me I'd really appreciate it.

The version of EPiServer.Community.dll that we're using is 3.2.517.24

Best regards


Jan 26, 2010 11:54

Ok, sorry, I somehow missed that you were saying that it actually did work when you did it the other way.

However, for me it works either way - I have also tested by making a simple webforms page with a <input type="file" id="fileImage" runat="server" />  input field and code like in my previous post but using fileImage.PostedFile.InputStream as the stream argument.

Jan 26, 2010 14:47

Does this happen in a context where MS AJAX is used? If so, that could also be something worth investigating...

Jan 26, 2010 14:57

Hello Håkan,

I guess it's just for me to admit that I was responsible for this problem myself :-) I wanted to have a better validation of uploaded files than just looking at the extension of the file and this messed things up. Apparently "looking at the stream" before uploading it wasn't that popular.

I've, at least temporarily, removed my extra check and now it works fine, thank  you for your time Håkan!!

Best regards


Jan 28, 2010 16:16

Hi Martin,

The solution to this issue is to reset the starting position of the input stream to 0. In your case you have to to do this.

postedFile.InputStream.Position = 0;

I was pondering over this for a few days now and had contacted EPiServer support. They pointed me to this post. I realised that even you are having the same issue.

Anyways, this solution works for me. Try this and do let me know if it works for you.


Apr 01, 2010 17:18

Hi all,

Is there a way to get URL of the original image (not thumbnail) from Image Gallery in EPiServer Community?

Thanks in advance

Jul 07, 2014 12:52
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.