LinkToolAddin/common/LocalResource.cs

47 lines
1.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace LinkToolAddin.common;
public class LocalResource
{
public static string ReadFileByResource(string resourceName)
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
return($"找不到嵌入资源:{resourceName}");
}
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
public static BitmapImage ReadImageByResource(string resourceName)
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
Console.WriteLine("资源未找到,请检查资源名称是否正确。");
return null;
}
// 如果是 WPF可以转换为 BitmapImage
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
return bitmap;
}
}
}