Reputation: 65
I am calling a function in my class library project from WorkerService class. Both apps are using .net core 3.1. I am compiling the code locally on Windows 10 and using Visual Studio 2019.
What am I exactly missing here?
Here's my code using PdfSharp
PdfDocument doc = new PdfDocument();
doc.Options.FlateEncodeMode = PdfFlateEncodeMode.BestCompression;
doc.Options.UseFlateDecoderForJpegImages = PdfUseFlateDecoderForJpegImages.Automatic;
doc.Options.NoCompression = false;
doc.Options.CompressContentStreams = true;
Image MyImage = Image.FromFile(imageLocation);
for (int PageIndex = 0; PageIndex < MyImage.GetFrameCount(FrameDimension.Page); PageIndex++)
{
MyImage.SelectActiveFrame(FrameDimension.Page, PageIndex);
XImage img = XImage.FromGdiPlusImage(MyImage);
var page = new PdfPage();
page.Size = PageSize.Letter;
if (MyImage.Width > MyImage.Height)
{
// In case the scanned POD is landscape, rotate the image by 90 degrees
MyImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
page.Rotate = 270;
}
doc.Pages.Add(page);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[doc.Pages.Count - 1]);
xgr.DrawImage(img, 0, 0, page.Width, page.Height);
xgr.Dispose();
}
doc.Save(Savepath);
doc.Close();
It is blowing up on this line
Image MyImage = Image.FromFile(imageLocation);
Upvotes: 4
Views: 29632
Reputation: 4573
The promise of .net standard was that you could consume the same assembly in .NET Framework, .NETCore and what is now .NET ("proper").
However, it is not its own runtime. It is more like just an interface that run on whatever runtime the application loads. Regrettably, .NETCore will implement the .NETStandard20 interface with a bunch of methods throwing exception and stating that so-and-so is not supported "on this platform". System.Drawing.Common is one of them since the underlying implementation in .NET Framework relied on the GDI+ stuff in windows.
So, the portability of .NETStandard is just kicking the can down the road. It has altered what would be compile checks into runtime problems. Even worse, this is a huge pitfall for transitive dependencies that all go under the flag of .NET Standard that may detonate at runtime.
I use .NET standard for creating portable C# libraries, but I am wary of referring to other .NET standard libraries that may do stuff that is not supported on some target runtime.
Upvotes: 0
Reputation: 91
You can get this error if your version of System.Drawing does not support the targeting framework version of your project. For example, if you are using the latest System.Drawing.Common, changing your target framework to netcore 8 does the trick.
Upvotes: 3
Reputation: 48964
The WHOLE concept of .net core is to reduce, if not outright remove dependances on windows code, and thus it becomes multi-platform, and even quite much CPU "processor" agnostic (allowing .net code to run on your phone, tablet, Linux, or even some small Raspberry device, and you don't even need a Intel/AMD processor for such code to run).
As such, you have to remove your dependences on windows.system.drawing of which PDF sharp uses. In fact, OFTEN using any non .net core library and reference is going to cause you much grief, since any .net library (non core) is going to often cause a dependency on windows.
I believe you can stick to text only, but if you are shoving in graphics, then you have to change your code.
there is a nuget package now here:
https://github.com/ststeiger/PdfSharpCore
It not 100% prime time ready, but some are reporting good success with that library.
A discussion of this issue can be found here:
https://github.com/empira/PDFsharp/issues/6
Upvotes: 2