#20644: Working around Azure Websites limitation of not being able to use System.Drawing.Image with .ico files.

WorkItem: 20644
This commit is contained in:
Lombiq
2014-10-31 19:50:44 +01:00
committed by Zoltán Lehóczky
parent c13929b566
commit 3a3655daaa

View File

@@ -29,7 +29,7 @@ namespace Orchard.MediaLibrary.Factories {
if (!String.IsNullOrEmpty(contentType)) {
var contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
if (contentDefinition == null || contentDefinition.Parts.All(x => x.PartDefinition.Name != typeof (ImagePart).Name)) {
if (contentDefinition == null || contentDefinition.Parts.All(x => x.PartDefinition.Name != typeof(ImagePart).Name)) {
return null;
}
}
@@ -65,12 +65,36 @@ namespace Orchard.MediaLibrary.Factories {
return null;
}
using (var image = Image.FromStream(stream)) {
imagePart.Width = image.Width;
imagePart.Height = image.Height;
try {
using (var image = Image.FromStream(stream)) {
imagePart.Width = image.Width;
imagePart.Height = image.Height;
}
}
catch (ArgumentException) {
// Still trying to get .ico dimensions when it's blocked in System.Drawing, see: https://orchard.codeplex.com/workitem/20644
if (mimeType != "image/x-icon" && mimeType != "image/vnd.microsoft.icon") {
throw;
}
TryFillDimensionsForIco(stream, imagePart);
}
return part;
}
private void TryFillDimensionsForIco(Stream stream, ImagePart imagePart) {
stream.Position = 0;
using (var binaryReader = new BinaryReader(stream)) {
// Reading out the necessary bytes that indicate the image dimensions. For the file format see:
// http://en.wikipedia.org/wiki/ICO_%28file_format%29
// Reading out leading bytes containing unneded information.
binaryReader.ReadBytes(6);
// Reading out dimensions. If there are multiple icons bundled in the same file then this is the first image.
imagePart.Width = binaryReader.ReadByte();
imagePart.Height = binaryReader.ReadByte();
}
}
}
}