Last active
January 10, 2022 03:12
-
-
Save yakumo-proj/7bc7a0bd64bc1b0d6613f6bde6e3e3bd to your computer and use it in GitHub Desktop.
vroidcustomitemをサムネイル表示 for .NET(SharpShell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using SharpShell.SharpThumbnailHandler; | |
using System.IO.Compression; | |
using SharpShell.Attributes; | |
using System.Runtime.InteropServices; | |
using System.Linq; | |
using System.IO; | |
namespace AlterCoord | |
{ | |
[ComVisible(true)] | |
[COMServerAssociation(AssociationType.ClassOfExtension, ".vroidcustomitem")] | |
public class VRoidCustomItemThumbnailer : SharpThumbnailHandler | |
{ | |
protected override Bitmap GetThumbnailImage(uint width) | |
{ | |
// Create a stream reader for the selected item stream | |
try | |
{ | |
using ZipArchive archive = new ZipArchive(SelectedItemStream, ZipArchiveMode.Read); | |
var thumbEntry = archive.GetEntry("thumbnails/thumbnail.png"); | |
using var stream = thumbEntry.Open(); | |
var img = Image.FromStream(stream); | |
stream.Close(); | |
var metaEntry = archive.GetEntry("v1customitem/meta.json"); | |
using var metaStream = metaEntry.Open(); | |
using var sr = new StreamReader(metaStream); | |
var category = sr.ReadToEnd(); | |
category = category.Split('.').Last().Split('\"').First(); | |
using Graphics g = Graphics.FromImage(img); | |
using Font fnt = new Font("Yu Gothic UI", 50, FontStyle.Bold); | |
//文字列を位置(0,0)、暗い赤で表示(雑な方法で白く縁取り) | |
g.DrawString(category, fnt, Brushes.White, -2, 0); | |
g.DrawString(category, fnt, Brushes.White, 2, 0); | |
g.DrawString(category, fnt, Brushes.White, 0, -2); | |
g.DrawString(category, fnt, Brushes.White, 0, 2); | |
g.DrawString(category, fnt, Brushes.DarkRed, 0, 0); | |
return new Bitmap(img, new Size((int)width, (int)width)); | |
} | |
catch (Exception exception) | |
{ | |
// Log the exception and return null for failure | |
LogError("An exception occurred opening the text file.", exception); | |
return null; | |
} | |
} | |
public VRoidCustomItemThumbnailer() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment