wpf 如何在 .NET 控制台应用程序中加载位图文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21863767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a bitmap file in a .NET console application
提问by user1741137
I'm trying to make a Console Application with C# that starts by loading an 8-bit gray level bitmap file (typically BMP) and transform it into a two dimensional byte array, where (as you would expect) the byte at position x,y is the intensity of pixel x,y. I then have a lot of code that will do some work on the bitmap as array.
我正在尝试使用 C# 制作一个控制台应用程序,它首先加载一个 8 位灰度位图文件(通常是 BMP)并将其转换为二维字节数组,其中(如您所料)位置 x 处的字节, y 是像素 x,y 的强度。然后我有很多代码可以对位图作为数组做一些工作。
The trouble is that I've seen this done with calls from WPF modules which just are not available in a console application. I don't want to use System.Windows.Media.Imagingfor example.
问题是我已经看到这是通过来自 WPF 模块的调用完成的,而这些模块在控制台应用程序中不可用。我不想使用System.Windows.Media.Imaging例如。
Does anyone have any suggestion as to how I can do this without too much trouble?
有没有人对我如何在没有太多麻烦的情况下做到这一点有任何建议?
回答by Daniel A.A. Pelsmaeker
You can add the System.Drawing.dllassembly to your project's references. Then you can use the System.Drawing.Bitmapclass.
您可以将System.Drawing.dll程序集添加到项目的引用中。然后就可以使用System.Drawing.Bitmap类了。
Add the following to the top of your code file to add the namespace System.Drawing:
将以下内容添加到代码文件的顶部以添加命名空间System.Drawing:
using System.Drawing;
To load a bitmap:
加载位图:
Bitmap bitmap = (Bitmap)Image.FromFile(@"mypath.bmp");
When you're done with the bitmap:
完成位图后:
bitmap.Dispose();
You can get the width, the height, and any pixels within the bitmap:
您可以获取位图中的宽度、高度和任何像素:
int width = bitmap.Width;
int height = bitmap.Height;
Color pixel00 = bitmap.GetPixel(0, 0);
回答by Bob Provencher
Use the System.Drawing.Bitmap ctor that takes a path to the image file.
使用获取图像文件路径的 System.Drawing.Bitmap ctor。

