繁体   English   中英

如何在 C# 中创建/编辑 PNG 文件?

[英]How do I create/edit a PNG file in C#?

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

在 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”确实存在于解决方案文件夹中。

首先,您必须了解创建 PNG 文件的分步过程。

  • 从 Nuget.org 为 .NET package 设置 Aspose.Imaging。
  • 包括对以下两个命名空间的引用:Aspose.Imaging,
    Aspose.Imaging.ImageOptions。
  • 转换前使用 SetLicense 方法指定许可证。
  • 将 BMP 文件读入图像 object。
  • 使用 PngOptions class 设置 output PNG 图像的属性。
  • 使用指定的 PNG 选项保存 output 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 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM