将图像转换为c#中的图标

时间:2013-06-20 11:41:07

标签: c# bitmap

我有一个将图像格式文件转换为图标文件的项目。但是,转换图像后,图像的颜色会发生变化。

这是我的代码

Bitmap theBitmap = new Bitmap(theImage, new Size(width, height));
IntPtr Hicon = theBitmap.GetHicon();// Get an Hicon for myBitmap.
Icon newIcon = Icon.FromHandle(Hicon);// Create a new icon from the handle.
FileStream fs = new FileStream(@"c:\Icon\" + filename + ".ico", FileMode.OpenOrCreate);//Write Icon to File Stream

enter image description here

有人知道如何解决这个问题吗?

7 个答案:

答案 0 :(得分:10)

Bitmap.GetHicon()非常擅长创建可在任何可运行.NET代码的Windows版本上运行良好的图标。包括旧版本,Windows 98和Windows 2000.尚未支持花哨图标的操作系统。

所以你得到的是一个只有 16种颜色的图标,使用带有基本颜色的预制调色板。这显然会产生令人失望的结果,温和地说。

Bitmap或Icon类没有获得更好结果的选项。通常,您需要使用图标编辑器来创建好的图标。其中应包括不同大小和颜色深度的多个图像,以便它们适用于任何视频适配器设置和任何操作系统版本。特别是从1600万色到256色或16色的色彩还原是一种非平凡的操作,有多种方法可以做到,它们都不是完美的。一个好的图标编辑器有你需要的工具,使它能够很好地工作。

答案 1 :(得分:6)

如果您只需要32位图标,则可以使用FreeImage http://freeimage.sourceforge.net

string icoFile = "C:\path\to\file.ico";
FreeImageBitmap fiBitmap = new FreeImageBitmap(theBitmap);
fiBitmap.Rescale(48, 48, FREE_IMAGE_FILTER.FILTER_BICUBIC);
fiBitmap.Save(icoFile);
fiBitmap.Rescale(32, 32, FREE_IMAGE_FILTER.FILTER_BICUBIC);
fiBitmap.SaveAdd(icoFile);
fiBitmap.Rescale(16, 16, FREE_IMAGE_FILTER.FILTER_BICUBIC);
fiBitmap.SaveAdd(icoFile);

如果要完全支持32,8,1和1位图标,则必须创建自己的ico格式编写器。我在开发自己的基于C#的png到ico转换器http://iconverticons.com

时遇到了这个问题

实际上并不太难;您需要的ico文件格式规范如下: http://msdn.microsoft.com/en-us/library/ms997538.aspx

您还需要此处的Bitmap标头规范,因为ico是位图的子集: http://msdn.microsoft.com/en-us/library/dd183376.aspx

答案 2 :(得分:1)

你可以试试这个:

Bitmap theBitmap = new Bitmap(theImage, new Size(width, height));

theBitmap.Save(@"C:\Icon\" + filename + ".ico", System.Drawing.Imaging.ImageFormat.Icon);

答案 3 :(得分:1)

这是我的方法,可以将png转换为图标,包括透明度:

public void ConvertToIco(Image img, string file, int size)
{
    Icon icon;
    using (var msImg = new MemoryStream())
    using (var msIco = new MemoryStream())
    {
        img.Save(msImg, ImageFormat.Png);
        using (var bw = new BinaryWriter(msIco))
        {
            bw.Write((short)0);           //0-1 reserved
            bw.Write((short)1);           //2-3 image type, 1 = icon, 2 = cursor
            bw.Write((short)1);           //4-5 number of images
            bw.Write((byte)size);         //6 image width
            bw.Write((byte)size);         //7 image height
            bw.Write((byte)0);            //8 number of colors
            bw.Write((byte)0);            //9 reserved
            bw.Write((short)0);           //10-11 color planes
            bw.Write((short)32);          //12-13 bits per pixel
            bw.Write((int)msImg.Length);  //14-17 size of image data
            bw.Write(22);                 //18-21 offset of image data
            bw.Write(msImg.ToArray());    // write image data
            bw.Flush();
            bw.Seek(0, SeekOrigin.Begin);
            icon = new Icon(msIco);
        }
    }
    using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
    {
        icon.Save(fs);
    }
}

答案 4 :(得分:1)

我创建了这个简单的函数,该函数将要转换的图像字符串和保存路径。 .ico可在chrome和其他浏览器上使用。

public void ConvertToIco(string pathImageToConvert, string pathToSave)
{
    Bitmap bmp = new Bitmap(pathImageToConvert);
    bmp.Save(pathToSave, System.Drawing.Imaging.ImageFormat.Icon);
}

这是路径的一个例子

pathImageToConvert = "yourpath/" + image.extension
pathToSave  = "yourpath/" + image.ico  

答案 5 :(得分:0)

似乎.directive('copyrightYear', function() { return { restrict: 'E', link: function(scope, element) { var date = new Date(); scope.year = date.getFullYear(); }, template: '{{year}}' }; }); 没有任何Icon Encoder;所以你不能保存任何真正的图标文件。保存的文件只是一个PNG文件。

答案 6 :(得分:0)

解决了Vista及更高版本

如果你必须动态生成图标(我的应用程序有不断变化的数字),你就可以了 如果你不得不使用编辑器,那就不走运了。

瑞威布(暗淡的降临)有一个github要点,展示如何。当然,我们当然不支持XP,而且这对我们来说也是如此。

使用类的示例我将该代码放在:

Dim ico As New Icons With {.state = Iconstates.OK, .UpgradeNum = "123"}
Dim tempfile = "C:\file5.ico"
ico.GetIcon(tempfile)
Dim newicon As New Icon(tempfile)

带着暗淡的代码的课程:

Imports System.Drawing.Imaging
Imports System.IO

Public Class Icons
  Property UpgradeNum As String
  Property state As Iconstates

  Function GetIcon(Optional OptionalSave As String = "") As Icon
    Dim bmp As New Bitmap(16, 16)

    Using g = Graphics.FromImage(bmp)
      g.Clear(Color.Transparent)
      g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
      Select Case state
        Case Iconstates.OK
          g.FillEllipse(Brushes.Green, 1, 1, 14, 14)
      End Select
      g.DrawString(UpgradeNum, New Font("Small Fonts", 6), Brushes.Aquamarine, 0, 0)
    End Using

    bmp.Save(OptionalSave & ".png")

    Dim outputStream As New MemoryStream()
    Dim size As Integer = bmp.Size.Width
    If Not ConvertToIcon(bmp, outputStream, size) Then
      Return Nothing
    End If
    If OptionalSave > "" Then

      Using file = New FileStream(OptionalSave, FileMode.Create, System.IO.FileAccess.Write)
        outputStream.WriteTo(file)
        file.Close()
      End Using
    End If

    outputStream.Seek(0, SeekOrigin.Begin)

    Return New Icon(outputStream)
  End Function




  ''' <summary>
  ''' Converts a PNG image to an icon (ico)
  ''' </summary>
  ''' <param name="inputBitmap">The input stream</param>
  ''' <param name="output">The output stream</param>
  ''' <param name="size">Needs to be a factor of 2 (16x16 px by default)</param>
  ''' <param name="preserveAspectRatio">Preserve the aspect ratio</param>
  ''' <returns>Wether or not the icon was succesfully generated</returns>
  Public Shared Function ConvertToIcon(inputBitmap As Bitmap, output As Stream, Optional size As Integer = 16, Optional preserveAspectRatio As Boolean = False) As Boolean

    Dim width As Single = size, height As Single = size


    Dim newBitmap = New Bitmap(inputBitmap, New Size(CInt(width), CInt(height)))
    If newBitmap Is Nothing Then
      Return False
    End If

    ' save the resized png into a memory stream for future use
    Using memoryStream As New MemoryStream()
      newBitmap.Save(memoryStream, ImageFormat.Png)

      Dim iconWriter = New BinaryWriter(output)
      If output Is Nothing OrElse iconWriter Is Nothing Then
        Return False
      End If

      ' 0-1 reserved, 0
      iconWriter.Write(CByte(0))
      iconWriter.Write(CByte(0))

      ' 2-3 image type, 1 = icon, 2 = cursor
      iconWriter.Write(CShort(1))

      ' 4-5 number of images
      iconWriter.Write(CShort(1))

      ' image entry 1
      ' 0 image width
      iconWriter.Write(CByte(width))
      ' 1 image height
      iconWriter.Write(CByte(height))

      ' 2 number of colors
      iconWriter.Write(CByte(0))

      ' 3 reserved
      iconWriter.Write(CByte(0))

      ' 4-5 color planes
      iconWriter.Write(CShort(0))

      ' 6-7 bits per pixel
      iconWriter.Write(CShort(32))

      ' 8-11 size of image data
      iconWriter.Write(CInt(memoryStream.Length))

      ' 12-15 offset of image data
      iconWriter.Write(CInt(6 + 16))

      ' write image data
      ' png data must contain the whole png data file
      iconWriter.Write(memoryStream.ToArray())

      iconWriter.Flush()
    End Using

    Return True
  End Function


End Class
' https://gist.github.com/darkfall/1656050
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================