gpt4 book ai didi

c# - 如何以编程方式更改有效的 Windows 图标?

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:51 25 4
gpt4 key购买 nike

我需要创建大约 10k 个不同的图标来进行测试,可以使用 C# 或 powershell。我实际上有 10k 个具有不同名称的相同图标文件,我认为我可以轻松地读入二进制图标,转换为字节,注入(inject)一些随机数并写回文件,但它并不像我看到的那样工作。

$fi = @(Get-ChildItem  D:\icons -rec  | ForEach-Object -Process {$_.FullName})  # | select -first $amount) 
$no = 0

foreach($i in $fi)
{
$array = Read-FileByte $i;
$array = $array + [System.Text.Encoding]::UTF8.GetBytes($no)
[System.IO.File]::WriteAllBytes($i, $array)
$no++
}

在此代码运行后,图标仍然被 Windows 认为是相同的。

另一种方法是以编程方式创建有效的 10k 图标,有没有办法做到这一点?谢谢

最佳答案

您可以使用此代码生成任意数量的随机图标:

using System;
using System.Linq;
using System.Drawing;
using System.IO;
static class Program
{
[STAThread]
static void Main()
{
var gen = new RandomIconGenerator(32);
var dir = new DirectoryInfo(@"C:\RandIcons\");
if (!dir.Exists) dir.Create();
for (int it = 0; it < 1000; it++)
using (var s = new FileStream(@"C:\RandIcons\" + "icon-" + it + ".ico", FileMode.Create))
gen.MakeRandomIcon().Save(s);
}
}
/// <summary>
/// Generates random icons using various colored shapes and lines, using available brushes and pens.
/// </summary>
public class RandomIconGenerator
{
Random r = new Random();
Pen[] pens = typeof(Pens).GetProperties().Select(p => (Pen)p.GetValue(null, null)).ToArray();
Brush[] brushes = typeof(Brushes).GetProperties().Select(p => (Brush)p.GetValue(null, null)).ToArray();
int size;
public RandomIconGenerator(int size) { this.size = size; }
public Icon MakeRandomIcon()
{
using (Bitmap bmp = new Bitmap(size, size))
using (Graphics g = Graphics.FromImage(bmp))
{
for (int it = 0; it < 20; it++) this.GetRandomPainter()(g);
g.Flush();
return Icon.FromHandle(bmp.GetHicon());
}
}
private Pen GetRandomPen() { return this.pens[this.r.Next(this.pens.Length)]; }
private Brush GetRandomBrush() { return this.brushes[this.r.Next(this.brushes.Length)]; }
private Action<Graphics> GetRandomPainter()
{
switch (r.Next(5))
{
case 0: return g => g.DrawLine(this.GetRandomPen(), this.GetRandomPoint(), this.GetRandomPoint());
case 1: return g => g.DrawRectangle(this.GetRandomPen(), this.GetRandomRect());
case 2: return g => g.DrawEllipse(this.GetRandomPen(), this.GetRandomRect());
case 3: return g => g.FillRectangle(this.GetRandomBrush(), this.GetRandomRect());
case 4: return g => g.FillEllipse(this.GetRandomBrush(), this.GetRandomRect());
default: throw new Exception();
}
}
private Rectangle GetRandomRect()
{
var p0 = this.GetRandomPoint();
return new Rectangle(p0, new Size(this.GetRandomPoint()) - new Size(p0));
}
private int GetRandomPos() { return this.r.Next(this.size); }
private Point GetRandomPoint() { return new Point(this.GetRandomPos(), this.GetRandomPos()); }
}

关于c# - 如何以编程方式更改有效的 Windows 图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7246641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com