是否可以选择在 .NET Standard 中使用 System.Drawing.Bitmap 而无需重写代码?
Is there an option for using System.Drawing.Bitmap in .NET Standard without rewriting code?
我正在尝试将我从 .NET Framework 4.7.2 编写的代码移植到 .NET Standard 2.0。该项目高度依赖 System.Drawing.Bitmap
个对象来处理图像数据,主要来自 System.IO.Stream
个对象。
有什么方法可以让我移植此代码而无需重写当前使用位图的所有内容?
我已经看过 所以我知道 .NET Standard 和 Bitmap 相处得不好,但我想知道是否有变通方法可以让我保留之前的现有代码我花了一个月重写依赖 Bitmap
.
的代码
有问题的代码主要集中在图像编辑和处理上。这是一个应该反转图像的函数。
Bitmap bmp;
...
public void Invert()
{
unsafe
{
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, Bitmap.Width, Bitmap.Height), ImageLockMode.ReadWrite, Bitmap.PixelFormat);
int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(Bitmap.PixelFormat) / 8;
int heightInPixels = bitmapData.Height;
int widthInBytes = bitmapData.Width * bytesPerPixel;
byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
for (int y = 0; y < heightInPixels; y++)
{
byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
{
currentLine[x] = (byte)(255 - currentLine[x]);
currentLine[x + 1] = (byte)(255 - currentLine[x + 1]);
currentLine[x + 2] = (byte)(255 - currentLine[x + 2]);
}
}
Bitmap.UnlockBits(bitmapData);
PixIsActive = false;
}
}
显然,BitmapData
和 Bitmap
会抛出错误,因为它们在此上下文中不存在。
为了完整起见,这是一个示例编译器错误 Visual Studio throws:
1>XImage.cs(4,22,4,29): error CS0234: The type or namespace name 'Imaging' does not exist in the namespace 'System.Drawing' (are you missing an assembly reference?)
是否有任何解决方法,或者这只是一个直接的 "you have to rewrite everything"?
只需安装 System.Drawing.Common
NuGet 包。它具有您需要的所有功能,并且 运行 支持 .NET Standard 2.0。
.NET Standard 仅提供有限的功能,这些功能在所有平台之间共享,这是制作程序所需的最低要求 运行。您可以通过 NuGet 包使用之前 .NET Framework 中的许多功能。
只需在 Visual Studio NuGet 包管理器中搜索您要使用的 class 名称,您很可能会找到合适的包。
我正在尝试将我从 .NET Framework 4.7.2 编写的代码移植到 .NET Standard 2.0。该项目高度依赖 System.Drawing.Bitmap
个对象来处理图像数据,主要来自 System.IO.Stream
个对象。
有什么方法可以让我移植此代码而无需重写当前使用位图的所有内容?
我已经看过 Bitmap
.
有问题的代码主要集中在图像编辑和处理上。这是一个应该反转图像的函数。
Bitmap bmp;
...
public void Invert()
{
unsafe
{
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, Bitmap.Width, Bitmap.Height), ImageLockMode.ReadWrite, Bitmap.PixelFormat);
int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(Bitmap.PixelFormat) / 8;
int heightInPixels = bitmapData.Height;
int widthInBytes = bitmapData.Width * bytesPerPixel;
byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
for (int y = 0; y < heightInPixels; y++)
{
byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
{
currentLine[x] = (byte)(255 - currentLine[x]);
currentLine[x + 1] = (byte)(255 - currentLine[x + 1]);
currentLine[x + 2] = (byte)(255 - currentLine[x + 2]);
}
}
Bitmap.UnlockBits(bitmapData);
PixIsActive = false;
}
}
显然,BitmapData
和 Bitmap
会抛出错误,因为它们在此上下文中不存在。
为了完整起见,这是一个示例编译器错误 Visual Studio throws:
1>XImage.cs(4,22,4,29): error CS0234: The type or namespace name 'Imaging' does not exist in the namespace 'System.Drawing' (are you missing an assembly reference?)
是否有任何解决方法,或者这只是一个直接的 "you have to rewrite everything"?
只需安装 System.Drawing.Common
NuGet 包。它具有您需要的所有功能,并且 运行 支持 .NET Standard 2.0。
.NET Standard 仅提供有限的功能,这些功能在所有平台之间共享,这是制作程序所需的最低要求 运行。您可以通过 NuGet 包使用之前 .NET Framework 中的许多功能。
只需在 Visual Studio NuGet 包管理器中搜索您要使用的 class 名称,您很可能会找到合适的包。