【问题标题】:How do I create/edit a PNG file in C#?如何在 C# 中创建/编辑 PNG 文件?
【发布时间】:2022-01-07 16:05:40
【问题描述】:

我编写了一个可以创建数字艺术的程序。像 Mandelbrot 集和 Julia 集这样的图像。但我希望将这些图像保存为 PNG。目前,在 Java 中,我在应用程序窗口中生成图像,然后对显示进行屏幕截图。但是,我失去了这些图像的更精细的细节。此外,这种方法还可以减小图像的物理尺寸。我希望能够用这些图片制作一张大海报。

在 C# 中,我使用以下内容: Bitmap myimage = new Bitmap("image.png"); 和:myimage.SetPixel(x,y, Color.FromArgb(255*colors[x,y], 255*colors[x,y], 255*colors[x,y]); 其中colors[,] 是介于 0 和 1 之间的某个值。

代码运行良好,减去 Bitmap 声明。我的理解是new Bitmap(filepath); 允许您编辑和操作PNG 图像。我这样想是对的吗?如何在 C# 中创建/编辑 PNG 文件?

(编辑)PS:PNG 文件“image.png”确实存在于解决方案文件夹中。

【问题讨论】:

  • “我这样想是对的吗?” - 试试看它是否有效。

标签: c# .net png


【解决方案1】:

首先你必须知道创建PNG文件的分步过程。

  • 为 Nuget.org 的 .NET 包设置 Aspose.Imaging。
  • 包括对以下两个命名空间的引用:Aspose.Imaging,
    Aspose.Imaging.ImageOptions。
  • 在转换前使用 SetLicense 方法指定许可证。
  • 将 BMP 文件读入 Image 对象。
  • 使用 PngOptions 类设置输出 PNG 图像的属性。
  • 使用指定的 PNG 选项保存输出的 PNG 图像。

从 BMP 创建 PNG 图像的代码

using System;
//Use following namespaces to create PNG image
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;

namespace CreatePNGImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Set license before creating PNG image from BMP
            Aspose.Imaging.License AsposeImagingLicense = new Aspose.Imaging.License();
            AsposeImagingLicense.SetLicense(@"c:\asposelicense\license.lic");

            //load input BMP image 
            Image BmpToPngImage = Image.Load("InputBMPImage.bmp");
            
            //set attributes of the output PNG file
            PngOptions PNGImageOptions = new PngOptions();
            PNGImageOptions.ResolutionSettings = new ResolutionSetting(300, 300);
            PNGImageOptions.CompressionLevel = 6;

            //save converted output PNG image
            BmpToPngImage.Save("OutputPNGImage.png", PNGImageOptions);

        }
    }
}

尝试使用 c# 创建 PNG 文件。

【讨论】:

    猜你喜欢
    • 2011-08-07
    • 2018-12-15
    • 2010-10-03
    • 2020-08-04
    • 2012-01-23
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    相关资源
    最近更新 更多