Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes System.Drawing from IdSharp.Tagging #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions IdSharp.Image/AttachedPictureWithImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using IdSharp.Common.Utils;
using IdSharp.Tagging.ID3v2.Frames;


namespace IdSharp.Tagging.ID3v2.Frames
{
// TODO: Unseal AttachedPicture for this extension to work.
/*
public class AttachedPictureWithImage : AttachedPicture, IAttachedPictureWithImage
{
private bool _loadingPicture;
private bool _pictureCached;
private Image _picture;

public AttachedPictureWithImage()
{
this.PropertyChanged += (string name) =>
{
if (name = nameof(IAttachedPicture.PictureData))
{
if (this.PictureData != null)
{
_picture = this.LoadPicture(ref _loadingPicture);
_pictureCached = true;
}
}
};
}

public Image Picture
{
get
{
if (_pictureCached == false)
{
_picture = this.LoadPicture(ref _loadingPicture);
_pictureCached = true;
}

return (_picture == null ? null : (Image)_picture.Clone());
}
set
{
if (_picture != value)
{
if (_picture != null)
{
_picture.Dispose();
}

_picture = value;

if (value == null)
{
this.PictureData = null;
}
else
{
this.SavePicture(value);
}
}

RaisePropertyChanged("Picture");
}
}

public string PictureExtension
{
get
{
if (_picture == null)
return null;

if (_picture.RawFormat.Equals(ImageFormat.Bmp))
return "bmp";
else if (_picture.RawFormat.Equals(ImageFormat.Emf))
return "emf";
else if (_picture.RawFormat.Equals(ImageFormat.Exif))
return null; // TODO - Unsure of MIME type?
else if (_picture.RawFormat.Equals(ImageFormat.Gif))
return "gif";
else if (_picture.RawFormat.Equals(ImageFormat.Icon))
return "ico";
else if (_picture.RawFormat.Equals(ImageFormat.Jpeg))
return "jpg";
else if (_picture.RawFormat.Equals(ImageFormat.MemoryBmp))
return "bmp";
else if (_picture.RawFormat.Equals(ImageFormat.Png))
return "png";
else if (_picture.RawFormat.Equals(ImageFormat.Tiff))
return "tif";
else if (_picture.RawFormat.Equals(ImageFormat.Wmf))
return "wmf";
else
return "";
}
}
*/
}
}
156 changes: 156 additions & 0 deletions IdSharp.Image/IAttachedPictureExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using IdSharp.Common.Utils;
using IdSharp.Tagging.ID3v2.Frames;


namespace IdSharp.Tagging.ID3v2.Frames
{
public static class AttachedPictureExtensions
{
public static void SavePicture(this IAttachedPicture attachedPicture, Image picture)
{
using (MemoryStream memoryStream = new MemoryStream())
{
picture.Save(memoryStream, picture.RawFormat);
attachedPicture.PictureData = memoryStream.ToArray();
}

SetMimeType(attachedPicture, picture);
}


private static void SetMimeType(IAttachedPicture attachedPicture, Image picture)
{
if (picture != null)
{
if (picture.RawFormat.Equals(ImageFormat.Bmp))
{
attachedPicture.MimeType = "image/bmp";
}
else if (picture.RawFormat.Equals(ImageFormat.Emf))
{
attachedPicture.MimeType = "image/x-emf";
}
else if (picture.RawFormat.Equals(ImageFormat.Exif))
{
// TODO - Unsure of MIME type?
}
else if (picture.RawFormat.Equals(ImageFormat.Gif))
{
attachedPicture.MimeType = "image/gif";
}
else if (picture.RawFormat.Equals(ImageFormat.Icon))
{
// TODO - How to handle this?
}
else if (picture.RawFormat.Equals(ImageFormat.Jpeg))
{
attachedPicture.MimeType = "image/jpeg";
}
else if (picture.RawFormat.Equals(ImageFormat.MemoryBmp))
{
attachedPicture.MimeType = "image/bmp";
}
else if (picture.RawFormat.Equals(ImageFormat.Png))
{
attachedPicture.MimeType = "image/png";
}
else if (picture.RawFormat.Equals(ImageFormat.Tiff))
{
attachedPicture.MimeType = "image/tiff";
}
else if (picture.RawFormat.Equals(ImageFormat.Wmf))
{
attachedPicture.MimeType = "image/x-wmf";
}
else
{
// TODO
//attachedPicture.MimeType = "image/";
}
}
}

public static Image LoadPicture(this IAttachedPicture attachedPicture)
{
bool _ = false;
return LoadPicture(attachedPicture, ref _);
}

public static Image LoadPicture(IAttachedPicture picture, ref bool loadingPicture)
{
loadingPicture = false;

var _pictureData = picture.PictureData;
if (_pictureData == null)
{
return null;
}

using (MemoryStream memoryStream = new MemoryStream(_pictureData))
{
bool isInvalidImage = false;
Image image = null;
try
{
loadingPicture = true;
try
{
image = Image.FromStream(memoryStream);
}
finally
{
loadingPicture = false;
}
}
catch (OutOfMemoryException)
{
string msg = string.Format("OutOfMemoryException caught in APIC's PictureData setter");
Trace.WriteLine(msg);

isInvalidImage = true;
}
catch (ArgumentException)
{
string msg = string.Format("ArgumentException caught in APIC's PictureData setter");
Trace.WriteLine(msg);

isInvalidImage = true;
}

if (isInvalidImage)
{
// Invalid image
if (image != null)
{
image.Dispose();
}

image = null;
try
{
string url = ByteUtils.ISO88591GetString(_pictureData);
if (url.Contains("://"))
{
picture.MimeType = "-->";
}
}
catch (Exception ex)
{
// don't throw an exception
Trace.WriteLine(ex);
}
}

return image;
}
}
}



}
19 changes: 19 additions & 0 deletions IdSharp.Image/IAttachedPictureWithImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Drawing;

namespace IdSharp.Tagging.ID3v2.Frames
{
public interface IAttachedPictureWithImage : IAttachedPicture
{
/// <summary>
/// Gets or sets the picture.
/// </summary>
/// <value>The picture.</value>
Image Picture { get; set; }

/// <summary>
/// Gets the picture extension.
/// </summary>
/// <value>The picture extension.</value>
string PictureExtension { get; }
}
}
15 changes: 15 additions & 0 deletions IdSharp.Image/IdSharp.Image.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.6.0-preview.19073.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IdSharp.Tagging\IdSharp.Tagging.csproj" />
</ItemGroup>

</Project>
Loading