简体   繁体   English

使用 C#,通过 URI 加载具有透明度的 .png 图像,裁剪它们并将它们绘制在画布上,然后将画布图像保存为 png 文件

[英]Using C#, load .png images with transparency via URI, crop them and draw them on a canvas, then save the canvas images as a png file

My goal is to load .png images with transparency via URI, crop them and draw them on a canvas, then save the canvas images as a png file.我的目标是通过 URI 加载具有透明度的 .png 图像,裁剪它们并将它们绘制在画布上,然后将画布图像保存为 png 文件。

In javascript, it would look like:在 javascript 中,它看起来像:

var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');

var img = new Image();
img.src = "a.png";

ctx.drawImage(img,10,10,20,20,30,30,10,10);

//drawing more images...

something(canvas.toDataURL('image/png'));

How could I do that in C# Visual Studios 2013?我怎么能在 C# Visual Studios 2013 中做到这一点? What would come the closest to this JS code?什么会最接近这个 JS 代码?

I don't mind using WPF or Winforms.我不介意使用 WPF 或 Winforms。 I do not need to be able to display the image.我不需要能够显示图像。 I only need to be able to save it.我只需要能够保存它。

One way to do it is with GDI+ (assumes using System.Drawing; ):一种方法是使用 GDI+(假设using System.Drawing; ):

using (var b = new Bitmap()) {            // This is your canvas
    Graphics g = Graphics.FromImage(b);   // This is your graphics context

    g.DrawImage(Image.FromFile("a.png"),
                new Rectangle(30, 30, 10, 10), 10, 10, 20, 20,
                GraphicsUnit.Pixel);

    // Do something with b (e.g. b.Save(…))
}

If you want the same data URI, it's (assumes using System.Drawing.Imaging; ):如果您想要相同的数据 URI,它是(假设using System.Drawing.Imaging; ):

using (var ms = new MemoryStream()) {
    using (var b = new Bitmap()) {
        // …

        b.Save(ms, ImageFormat.Png);
    }

    string base64 = Convert.ToBase64String(ms.ToArray());
    something("data:image/png;base64," + base64);
}

You can use WPF as an image generator.您可以使用 WPF 作为图像生成器。

The namespaces you'll need include:您需要的命名空间包括:

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

And a sample snippet of code to get you started:还有一个示例代码片段可以帮助您入门:

DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();

// Let's draw a rectangle!
var gradientBrush = new LinearGradientBrush();
gradientBrush.GradientStops.Add(new GradientStop(Colors.Azure, 0.0));
gradientBrush.GradientStops.Add(new GradientStop(Colors.SteelBlue, 1.0));
drawingContext.DrawRectangle(gradientBrush, null, new Rect(0, 0, _imageWidth, _imageHeight));



drawingContext.Close();

// Now to save it
RenderTargetBitmap bmp = new RenderTargetBitmap(_imageWidth, _imageHeight, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmp));

byte[] imageBinary = null;
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
    png.Save(memoryStream);
    imageBinary = memoryStream.GetBuffer();
}

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

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