PDFsharp image to XImage
Asked Answered
F

4

6

I created a barcode as an image in my ASP.NET MVC app.

BarcodeLib.Barcode barcode = new BarcodeLib.Barcode()                   
{                        
    IncludeLabel = false,
    Alignment = AlignmentPositions.LEFT,
    Width = element.BarcodeWidth,
    Height = element.BarcodeHeight,
    RotateFlipType = RotateFlipType.RotateNoneFlipNone,
    BackColor = Color.Transparent,
    ForeColor = Color.Black,
    ImageFormat = System.Drawing.Imaging.ImageFormat.Png
 };

This will create a barcode with the BarcodeLib.
How do I convert it to an XImage of PDFsharp?

Image img = barcode.Encode(TYPE.CODE128, "123456789");
gfx.DrawImage(xImage, 0, 0, 100, 100);
Fulani answered 3/6, 2017 at 19:29 Comment(0)
F
9

Solved it this way:

Image img = barcode.Encode(TYPE.CODE128, Name); // this is the image

MemoryStream strm = new MemoryStream();
img.Save(strm, System.Drawing.Imaging.ImageFormat.Png);

XImage xfoto = XImage.FromStream(strm);
Fulani answered 3/6, 2017 at 22:33 Comment(0)
S
4

Actually method had been changed in .net core version. It takes Func as parameter. Attention please: It's unofficial version for .net core.

XImage xImage = XImage.FromStream(() => new MemoryStream(yourByte[]));
Sedgemoor answered 19/5, 2020 at 10:55 Comment(3)
Are you sure? Please look at the method, after then we can discuss about thatSedgemoor
Looks like an inofficial port of PDFsharp (I don't know which). Most likely it didn't exist in 2017 when the question was asked. Not useful to illustrate special extensions of special ports without clearly identifying the port they belong to.Funch
That's right, It's an unofficial but this is so useful for .net core otherwise you have to change most of things. So please correct your scoring.Sedgemoor
F
3

If you use the GDI build of PDFsharp then you can call the XImage.FromImage method.

With any build of PDFsharp you can write an PNG image to a MemoryStream and then get an XImage from that MemoryStream.

Funch answered 3/6, 2017 at 19:42 Comment(1)
There is also XImage.FromStream, so you can add images created in a MemoryStream. You'll need version 1.5 for that.Peroxidize
S
1

Also, If you know image path you can use that

XImage xImage = XImage.FromFile(imagePath)
Sedgemoor answered 12/5, 2020 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.