簡體   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