Microsoft
How To Convert Bitmap to BitmapImage
01/04/2025 Filed in: Windows | Recent Apps
How To Convert System.Drawing.Bitmap to Microsoft.UI.Xaml.Media.Imaging.BitmapImage
Add the Function 'ConvertBitmapToBitmapImage':
Add the Function 'GetBitmapImageFromBitmapBytes':
// get bitmapimage
var bitmapImage = await ConvertBitmapToBitmapImage(app.AppIcon);
// determine bitmapimage not null
if (bitmapImage != null) {
// set app icon
appItem.AppIcon = bitmapImage;
}
Add the Function 'ConvertBitmapToBitmapImage':
// convert bitmap to bitmapimage
public static async TaskBitmapImage> ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap) {
// determine bitmap not null
if (bitmap == null)
// return null
return null;
// create new bitmapimage
Microsoft.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new();
// create imagestream
var imageStream = new MemoryStream();
// using imagestream
using (imageStream) {
// save bitmap as jpeg
bitmap.Save(imageStream, ImageFormat.Jpeg);
imageStream.Position = 0;
// convert stream to bytes array
byte[] imageBytes = imageStream.ToArray();
// get bitmapimage from bytes
bitmapImage = await GetBitmapImageFromBitmapBytes(imageBytes);
}
// return bitmapimage
return bitmapImage;
}
Add the Function 'GetBitmapImageFromBitmapBytes':
// get bitmapimage from bitmap bytes
public static async TaskBitmapImage> GetBitmapImageFromBitmapBytes(byte[] data) {
// create new bitmapimage
Microsoft.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new();
// using memorystream
using (var stream = new InMemoryRandomAccessStream()) {
// using datawriter
using (var writer = new DataWriter(stream)) {
// write bytes
writer.WriteBytes(data);
// store
await writer.StoreAsync();
// flush
await writer.FlushAsync();
// detach
writer.DetachStream();
}
// set stream to 0
stream.Seek(0);
// set bitmapimage source from stream
await bitmapImage.SetSourceAsync(stream);
}
// return bitmapimage
return bitmapImage;
}
How To Get Microsoft Windows App Store app's icon image from .exe:
01/04/2025 Filed in: Windows | Recent Apps
How To Get Microsoft Windows App Store app's icon image from .exe:
Create a public variable:
Create a custom class
Setup code to get the apps folder via the apps folder ID.
Enumerate through the apps folder and create vars using the above created 'AppStoreItem' class.
Add each AppStoreItem var to the above created public var 'AppFolderList'.
Create a public variable:
List<AppStoreItem> AppFoldersList { get; set; } = new();
Create a custom class
// appstoreitem class
public class AppStoreItem {
public string AppName { get; set; }
public Bitmap AppIcon { get; set; }
}
Setup code to get the apps folder via the apps folder ID.
Enumerate through the apps folder and create vars using the above created 'AppStoreItem' class.
Add each AppStoreItem var to the above created public var 'AppFolderList'.
// GUID taken from https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
var appsFolderID = new Guid("{1e87508d-89c2-42f0-8a7e-645a0f50ca58}");
// get appsfolder
ShellObject appsFolder = (ShellObject)KnownFolderHelper.FromKnownFolderId(appsFolderID);
// enumerate appsfolder
foreach (var iknownFolder in (IKnownFolder)appsFolder) {
// create new appstoreitem
AppStoreItem appStoreItem = new();
appStoreItem.AppName = iknownFolder.ToString();
appStoreItem.AppIcon = iknownFolder.Thumbnail.Bitmap;
// add appstoreitem to appfolderslist
AppFoldersList.Add(appStoreItem);
}