Arumugam
Arumugam

Reputation: 23

How to return an image from a controller method?

I want to take the mp3 albumart in the mp3 file, using TagLib. I am using that code, it gives the System.Drawing.Bitmap. How to display in the Webpage. I am Using MVC.

var file12 = TagLib.File.Create(file);

if (file12.Tag.Pictures.Length >= 1)
{
    var bin = (byte[])(file12.Tag.Pictures[0].Data.Data);
    if (bin.Length > 0)
    {
        Images = System.Drawing.Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(200, 200, null, IntPtr.Zero);
        album = file12.Tag.Album;
    }
}

Please Help me

Upvotes: 2

Views: 2698

Answers (3)

Adriano Repetti
Adriano Repetti

Reputation: 67148

Just put that code in a controller method, image will be loaded asynchronously (in this example controller method accepts file name as parameter, it may not be your case):

public ActionResult GetImage(string file)
{
    var file12 = TagLib.File.Create(file);
    if (file12.Tag.Pictures.Length >= 1)
    {
        string fileName = Path.ChangeExtension(
            Path.GetFileName(file), ".jpg");

        return base.File(
            (byte[])(file12.Tag.Pictures[0].Data.Data),
            "image/jpeg", fileName);
    }

    // You have to handle this case
    return null;
}

Controller.File() method will do all the dirty job for you. For clarity in this example I omitted rescaling, just copy & paste your code there if needed. Please note you have to return something when no image is not available (a default thumbnail?), you may even return a HTTP error with:

return HttpNotFound();

Your HTML will be for example like this:

<img
     src='@Url.Action("GetImage", new { file = "filenamehere.mp3" })'
     alt='thumbnail' />

Please note that here I assumed your image is in JPG format, if it's not you may need to convert it to a known format to return proper MIME type (to detect MIME type from byte stream is possible too, check this post here on SO).

Upvotes: 2

Arumugam
Arumugam

Reputation: 23

I have Choose an Another option for display AlbumArt of the Mp3 file.

Create Windows Form Application for Tracking mp3 file Album Art.

   var filename = TagLib.File.Create(file);
                        if (filename.Tag.Pictures.Length >= 1)
                        {
                            var bin = (byte[])(filename.Tag.Pictures[0].Data.Data);
                            if (bin.Length > 0)
                            {
                                Images = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(800, 800, null, IntPtr.Zero);
                                PreviewPictureBox.Image = Images;
                                PreviewPictureBox.Image.Save("C:/Users/v-manshr/Desktop/MyMusicPlayer/MyMusicPlayer/AlbumImage/"+filename.Tag.Album+".jpg");
                                PreviewPictureBox.Visible = false;
                            }
                        }

Here PreviewPictureBox is a WindwosFormComtrol.

now i got a mp3 file album art, save in one location.now in MVC controller i copy that all files in to my Visual Studio Local Folders.Using (File.Copy).now u Display in Viewr.using Image Src tag

Upvotes: 0

Dipal Mehta
Dipal Mehta

Reputation: 462

You can try Base64 converted string to get Image with Ajax or regular call

FYI. http://www.codeproject.com/Articles/201767/Load-Base64-Images-using-jQuery-and-MVC

Hope helps.

Upvotes: 0

Related Questions