簡體   English   中英

如何以編程方式更改有效的Windows圖標?

[英]how to alter valid windows icon programmatically?

我需要創建類似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圖標,有沒有辦法做到這一點? 謝謝

您可以使用此代碼生成任意數量的隨機圖標:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM