c# - 如何将元文件的颜色设置为特定值?
问题描述
我需要将元文件中所有可用对象的颜色更改为特定颜色。但是,当前提供的仅用于平移/缩放/旋转颜色。例如,我需要将所有可用对象的颜色设置为绿色。
下面目前讨论的是只是相对地改变颜色,而不是绝对地改变颜色:
https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/recoloring-images
这段代码可以做的只是将所有颜色转换为白色。但我需要将所有颜色设置为例如绿色、深红色等。有没有办法做到这一点?
ImageAttributes GetAttr()
{
/* Get image attributes to translate to white */
float[][] colorMatrixElements = {
new float[] {1, 0, 0, 0, 0}, // no red scaling
new float[] {0, 1, 0, 0, 0}, // no green scaling
new float[] {0, 0, 1, 0, 0}, // no blue scaling
new float[] {0, 0, 0, 1, 0}, // no alpha scaling
new float[] {1, 1, 1, 1, 1}}; // four translations of 1.0
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(new ColorMatrix(colorMatrixElements));
return imageAttributes;
}
void test()
{
var fname = Path.GetTempFileName();
var frame = new Rectangle(0, 0, 640, 480);
using (var img = new Metafile(fname, CreateGraphics().GetHdc(), frame, MetafileFrameUnit.Point))
{
using (var g = Graphics.FromImage(img))
{
var arrow = new Metafile("arrow.emf");
g.DrawImage(arrow, new Rectangle(150, 400, 75, 50),
0, 0, arrow.Width, arrow.Height, GraphicsUnit.Pixel, GetAttr());
}
}
}
解决方案
推荐阅读
- python-3.x - 如何在不连接到计算机上活动实例的情况下运行 Python 脚本
- javascript - 使条纹不适用于测试信用卡号
- python - 如何在 Python 中的文件路径中间插入目录?
- php - 如何从具有多个相同数组对象的json中解析数据
- azure - 如何将 Terraform 与 Azure CLI 和 REST API 结合使用?
- javascript - 打开新选项卡后,如何切换到 Puppeteer 浏览器实例中的上一个选项卡?
- jsp - 使用 ModelDriven Interceptor 请求参数未映射到 struts 2 中的模型类
- performance - 为什么当 goroutine 争用超过 3400 时,sync.Mutex 会大幅降低性能?
- rest - 使用 HATEOAS 批量获取
- excel - 有没有公式可以用excel中的另一串数字替换一串数字?