Get Albumart in .mp3 file for Windows Store App - c#

How can i get the AlbumArt image in the mp3 file? I am developing Windows Store app with c#.
MusicProperties class gives me Album name artist name vs. But it cant give me albumart.

Check out MSDN sample to show thumbnail of any file. It also consists how to retrieve the album art.
File and folder thumbnail sample
If you want to save the album art check out How to store save Thumbnail image in device in windows 8 metro apps c#
UPDATE 1
MediaFile is StorageFile. ImageControl is <Image ... />
using (StorageItemThumbnail thumbnail = await MediaFile.GetThumbnailAsync(ThumbnailMode.MusicView, 300))
{
if (thumbnail != null && thumbnail.Type == ThumbnailType.Image)
{
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(thumbnail);
ImageControl.Source = bitmapImage;
}
}

here's my quick and short solution for that problem thirding TagLib#: (http://taglib.github.io/)
using TagLib;
var file = TagLib.File.Create(filename);
var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
imageBox.Image = Image.FromStream(new MemoryStream(bin));

I found a solution here How to: Get IDE Tags and Thumbnails of a file
var bitmapImage = new BitmapImage();
//Get Album cover
using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.MusicView, 300))
{
if (thumbnail != null && thumbnail.Type == ThumbnailType.Image)
{
bitmapImage.SetSource(thumbnail);
}
else
{
//Error Message here
}
}

Related

Xamarin.Essentials Share drawable

I'm trying to share a local image loacted in my Resources/Drawables folder in Android.
I'm using Xamarin and the Xamarin.Essentials plugin.
So there is this function:
await Share.RequestAsync(new ShareFileRequest
{
Title = Title,
File = new ShareFile(file)
});
So for the File I need the Path to the File from the Drawable Folder.
I have tried so much variations.
For example:
var file = Android.Net.Uri.Parse("android.resource://" + Android.App.Application.Context.PackageName + "/" + Resource.Drawable.image).Path;
But I always get an error, that the file is not found.
What I'm doing wrong?
Thanks in advance
Because Resources don't have file paths, as #Jason said I saved the image in another accesible file.
So I don't know if this is the best solution, but it works for me:
Drawable drawable = ResourcesCompat.GetDrawable(Resources, Resource.Drawable.image, null);
Bitmap bitmap = ((BitmapDrawable)drawable).Bitmap;
byte[] imgByteArray;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
imgByteArray = stream.ToArray();
}
var file = System.IO.Path.Combine(FileSystem.CacheDirectory, "share.png");
File.WriteAllBytes(file, imgByteArray);
await Share.RequestAsync(new ShareFileRequest
{
Title = "Share",
File = new ShareFile(file)
});

UWP image picker not working for image source

I'm trying to get image picker to work, and it does, but for some reason it won't populate as an image.
var openPicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.PicturesLibrary
};
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
//Image img = new Image();
userImage.Source = new BitmapImage(new Uri(file.Path));
//await ProcessFile(file);
}
and the image is just simply:
<image name="userImage" height="500px" width="500px"/>
This isn't work because the UWP applications have permissions only for some users folders and even you need to browse their files, you need to specify it in the package.manifest of your application which folders you want to access. For simplification, you could create a copy of the file inside the application data folder and get the path from there or set the image source from the stream of the file, but be careful, the second option maybe lead some high memory usage and leaks. You can find how to avoid this here.
You can set stream as source instead of path.
var result = new BitmapImage();
using (var randomAccessStream = await file.OpenAsync(FileAccessMode.Read))
{
await result.SetSourceAsync(randomAccessStream);
}

C# Xamarin.Android images are displayed in the file manager and not in the gallery

I made an application to save photos on the phone.
public void DownloadImage(string linkbitmap, string title)
{
Bitmap bitmap = GetImageBitmapFromUrl(linkbitmap).Result;
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures + "/Tsundere");
var complete = path.AbsolutePath;
var filepatch = System.IO.Path.Combine(complete, title + ".jpg");
var filestream = new FileStream(filepatch, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 100, filestream);
filestream.Close();
}
The pictures are displayed in the file manager, but in the example the discord application does not have them. I have to restart the phone to be visible. How to make photos immediately visible in other applications, such as photos from the camera or photos downloaded from facebook?
I solve this problem. Just add MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] {path}, null, null);in the code
description this class https://developer.xamarin.com/api/type/Android.Media.MediaScannerConnection/
Construct an Intent action (Intent.ActionMediaScannerScanFile) with a uri that includes the file path:
using (var file = new Java.IO.File(path))
using (var uri = Android.Net.Uri.FromFile(file))
{
var scanFileIntent = new Intent(Intent.ActionMediaScannerScanFile, uri);
SendBroadcast(scanFileIntent);
}
FYI: You can create/register a BroadcastReceiver that listens for Intent.ActionMediaScannerFinished to determine when the media scanner is done processing your request.

MP3 ID3 Read Cover Art: Can only read embedded

I've tried every Library i could find, and been searching StackOverflow and google for days without getting it working.
Libraries: UltraID3Lib, TagLib, IdSharp, ID3 Tag Library by Perry Butler, ID3Lib, C# ID3 Library, ID3.Net, ... ...
None of these seem to be able to get the Cover Art that winamp shows the file has. There is no cover art or jpg or anything of the sort in the folder containing the mp3 either.
Only embedded Cover Art is showing up in every library, but other are not.
When opening winamp mp3 "File Info", if the "origin" displays "embedded" it shows up in every library, but if it shows origin "folder" then only winamp seems to know about it. Mind you, there are no images in any folder at all, the mp3 itself does contain this image.
So far i only found 1 other compiled application that gets these images, but i have no way of finding out how they did it.
Anyone know what I'm doing wrong? (I am using TagLib atm: https://github.com/mono/taglib-sharp)
public Song(string filePath)
{
var id3 = TagLib.File.Create(filePath);
TrackNumber = (Winamp.PlaylistPosition + 1);
Artist = id3.Tag.FirstArtist;
Title = id3.Tag.Title;
Position = Winamp.GetTrackPosition();
Length = Winamp.GetTrackLength();
AlbumArt = LoadPicture(id3);
Status = Winamp.GetPlaybackStatus();
}
private Bitmap LoadPicture(TagLib.File file)
{
Image currentImage = null;
if (file.Tag.Pictures.Length > 0)
{
TagLib.IPicture pic = file.Tag.Pictures[0];
MemoryStream ms = new MemoryStream(pic.Data.Data);
if (ms.Length > 0)
currentImage = Image.FromStream(ms);
ms.Close();
}
return (Bitmap)currentImage;
}

Bind images to listbox with datatemplate from app package folder

Hi I need bind images to listbox but when I try it I get FILE NOT FOUND but file is stored in application package in folder layoutGraphics. I try put files to default folder but I get same result anyone know what is bad?
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("ms-appx:///layoutGraphics/offline.png");
var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var img = new BitmapImage();
img.SetSource(fileStream);
ImgSource = img;
// property
private BitmapImage _imgSource;
public BitmapImage ImgSource
{
get { return _imgSource; }
set
{
_imgSource = value;
OnPropertyChanged("MyDatasMessagesUserList");
}
}
Or anyone know better solution how I can bind imagess from app folder to my listbox with datatemplate?
Windows.Storage.ApplicationData.Current.LocalFolder is retriving file from the application storage not the package. For the package folder you need to use Windows.ApplicationModel.Package.Current.InstalledLocation. Also the GetFileAsync take just the name of a file not a full path.
Here is the code to acomplish what you want:
var layoutGraphiceFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("layoutGraphics")
var file=await layoutGraphiceFolder.GetFileAsync("offline.png");
Another way to do it with the full path is:
var file=await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///layoutGraphics/offline.png"));

Categories

Resources