如何从 System.Drawing.Color 转换为 System.Windows.Media.Color?
Posted
技术标签:
【中文标题】如何从 System.Drawing.Color 转换为 System.Windows.Media.Color?【英文标题】:How to convert from System.Drawing.Color to System.Windows.Media.Color? 【发布时间】:2011-06-07 10:13:12 【问题描述】:如何在这两种颜色类型之间进行转换?
【问题讨论】:
System.Drawing.Color => System.Windows.Media.Color 的可能重复项 【参考方案1】:您可以在下面查看两种转换方向的示例:
Drawing.Color
到 Windows.Media.Color
// This is your color to convert from
System.Drawing.Color color;
System.Windows.Media.Color newColor = System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
Windows.Media.Color
到 Drawing.Color
// This is your color to convert from
System.Windows.Media.Color color;
System.Drawing.Color newColor = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
【讨论】:
【参考方案2】:...或使用@Rion Williams 回答作为扩展名:
public static System.Windows.Media.Brush ToBrush(this System.Drawing.Color color)
return new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
【讨论】:
【参考方案3】:为了经常使用,我建议这样的助手:
using SDColor = System.Drawing.Color;
using SWMColor = System.Windows.Media.Color;
namespace ColorHelper
public static class ColorExt
public static SWMColor ToSWMColor(this SDColor color) => SWMColor.FromArgb(color.A, color.R, color.G, color.B);
public static SDColor ToSDColor(this SWMColor color) => SDColor.FromArgb(color.A, color.R, color.G, color.B);
【讨论】:
【参考方案4】:这是我用的:
using System.Runtime.CompilerServices;
using System.Windows.Media;
namespace HQ.Wpf.Util
public static class ColorExtension
/// <summary>
/// Convert Media Color (WPF) to Drawing Color (WinForm)
/// </summary>
/// <param name="mediaColor"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static System.Drawing.Color ToDrawingColor(this System.Windows.Media.Color mediaColor)
return System.Drawing.Color.FromArgb(mediaColor.A, mediaColor.R, mediaColor.G, mediaColor.B);
/// <summary>
/// Convert Drawing Color (WPF) to Media Color (WinForm)
/// </summary>
/// <param name="drawingColor"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static System.Windows.Media.Color ToMediaColor(this System.Drawing.Color drawingColor)
return System.Windows.Media.Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B);
【讨论】:
以上是关于如何从 System.Drawing.Color 转换为 System.Windows.Media.Color?的主要内容,如果未能解决你的问题,请参考以下文章
在 C# 中,如何使用十六进制值创建 System.Drawing.Color 对象? [复制]
如何将十六进制#FFFFFF 转换为 System.Drawing.Color [重复]
System.Drawing.Brush 来自 System.Drawing.Color