C# png转ICon
时间: 2023-10-19 12:36:16 浏览: 62
你可以使用C#的System.Drawing命名空间来将PNG图像转换为ICO图标。下面是一个简单的示例代码:
```csharp
using System.Drawing;
using System.Drawing.Imaging;
// 将PNG图像转换为ICO图标
public static void ConvertPngToIcon(string pngFilePath, string icoFilePath)
{
// 读取PNG图像
Image pngImage = Image.FromFile(pngFilePath);
// 创建一个空的ICO图标
Icon icon = Icon.FromHandle(new Bitmap(pngImage, new Size(256, 256)).GetHicon());
// 保存ICO图标到文件
using (var stream = new System.IO.FileStream(icoFilePath, System.IO.FileMode.Create))
{
icon.Save(stream);
}
}
```
你只需将`pngFilePath`参数设置为输入的PNG图像文件路径,`icoFilePath`参数设置为要保存的ICO图标文件路径。然后调用`ConvertPngToIcon`方法即可将PNG图像转换为ICO图标。
相关问题
c# png 转jpg 白底
要使用C#将PNG转换为JPG并添加白色背景,可以使用System.Drawing命名空间中的类。以下是一个简单的代码示例:
```csharp
using System.Drawing;
using System.Drawing.Imaging;
public void ConvertPngToJpgWithWhiteBackground(string pngFilePath, string jpgFilePath)
{
// Load the PNG image
using (Image pngImage = Image.FromFile(pngFilePath))
{
// Create a new bitmap with white background
using (Bitmap whiteBgBitmap = new Bitmap(pngImage.Width, pngImage.Height, PixelFormat.Format24bppRgb))
{
using (Graphics graphics = Graphics.FromImage(whiteBgBitmap))
{
graphics.Clear(Color.White);
graphics.DrawImage(pngImage, 0, 0);
}
// Save the bitmap as JPG
whiteBgBitmap.Save(jpgFilePath, ImageFormat.Jpeg);
}
}
}
```
这个方法将加载指定路径的PNG图像,创建一个新的具有白色背景的位图,并将PNG图像绘制到该位图上。最后,它将保存新的JPG图像到指定的路径。
c# png 转 jpg 白底
c#中可以使用System.Drawing命名空间下的Image类来进行图片格式转换。具体步骤如下:
1.使用Image.FromFile方法读取png图片;
2.创建一个新的Bitmap对象,并将png图片绘制到该对象上;
3.使用Bitmap.Save方法将图片保存为jpg格式。
下面是代码示例:
```
using System.Drawing;
using System.Drawing.Imaging;
public static void ConvertPngToJpg(string pngFilePath, string jpgFilePath)
{
// 读取png图片
Image pngImage = Image.FromFile(pngFilePath);
// 创建一个新的Bitmap对象,并将png图片绘制到该对象上
Bitmap bitmap = new Bitmap(pngImage.Width, pngImage.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(pngImage, new Rectangle(0, 0, pngImage.Width, pngImage.Height));
// 将图片保存为jpg格式
bitmap.Save(jpgFilePath, ImageFormat.Jpeg);
// 释放资源
pngImage.Dispose();
graphics.Dispose();
bitmap.Dispose();
}
```