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.
try
{
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.");
}
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 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).
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
Martin
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.
Does this happen in a context where MS AJAX is used? If so, that could also be something worth investigating...
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
Martin
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.
Regards,
Vijay
Hi all,
Is there a way to get URL of the original image (not thumbnail) from Image Gallery in EPiServer Community?
Thanks in advance
Hello,
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 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
Martin