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

发布于 2024-12-02 07:01:21 字数 535 浏览 1 评论 0原文

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

$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 图标, 有办法做到这一点吗? 谢谢

I need create like 10k different icons for testing, can be in C# or powershell.
I have actually 10k identical icons files with different names and I thought I could easily read binary icon in, convert to bytes inject some random number and write back to a file but it doesn't work that way as I see.

$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++
}

After this code run icons still are considered by windows identical.

ALternative way would be creating valid 10k icons programmatically,
is there a way to do this?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无可置疑 2024-12-09 07:01:21

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

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()); }
}

You can use this code to generate as many random icons as you like:

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()); }
}
夕色琉璃 2024-12-09 07:01:21
//fixed
[DllImport("user32.dll", SetLastError = true)] static extern bool DestroyIcon(IntPtr hIcon);
       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.Dispose();
               IntPtr hIcon = bmp.GetHicon();
               Icon temp = Icon.FromHandle(hIcon);
               Icon ico = (Icon)temp.Clone();
               temp.Dispose();
               DestroyIcon(hIcon);
               return ico;
           }
       }


//thanks Miguel again.
//fixed
[DllImport("user32.dll", SetLastError = true)] static extern bool DestroyIcon(IntPtr hIcon);
       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.Dispose();
               IntPtr hIcon = bmp.GetHicon();
               Icon temp = Icon.FromHandle(hIcon);
               Icon ico = (Icon)temp.Clone();
               temp.Dispose();
               DestroyIcon(hIcon);
               return ico;
           }
       }


//thanks Miguel again.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文