LeadTools在图像中设定窗位的具体方法

2026-02-28 01:09:29

1、打开Visual Studio .NET。点击 文件->新建->项目…。打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“WindowLevel”,并使用“浏览”按钮选冷没佛择您工程的存储路径,点击“确定”。

2、在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。根据当前工程的 Framework 版本和生成目标平台,膨捕选择添加相应的LeadTools控件,例如工程中的版本为 Framework 4.0、生成目标平台是 x86,则浏览选择Leadtools For .NET文件夹” <LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32”,选择以下的DLL“:

Leadtools.dll

Leadtools.Codecs.dll

Leadtools.Codecs.Bmp.dll

Leadtools.WinForms.dll

Leadtools.ImageProcessing.Core.dll      

Leadtools.ImageProcessing.Color.dll      

Leadtools.ImageProcessing.Utilities.dll      

Leadtools.Dicom.dll

Leadtools.Drawing.dll

点击“确定”按钮,将以上所有的DLL添加到应用程序中。

3、从工具箱(视图->工具箱),添加两个Button控件(将button1的Text属性修改为“加载图像”,将button2的Text属性修改为“设定窗位并保存图像”),一个Panel控件(Name修改为panelImage)。如下图:

LeadTools在图像中设定窗位的具体方法

4、切换至Form1的代码视图(右击Form1,选择查看代码),将关耍下面几行代码添加到文件开始处:

  1: using Leadtools;

  2: using Leadtools.Codecs;

  3: using Leadtools.ImageProcessing;

  4: using Leadtools.ImageProcessing.Core;

  5: using Leadtools.ImageProcessing.Color;

  6: using Leadtools.WinForms;

  7: using Leadtools.Dicom;

  8: using Leadtools.Drawing; 

5、将以下变量添加至Form1类:

  1: private RasterCodecs codecs;

  2: private RasterImage image;

  3: private RasterImageViewer imageViewer;

6、双击“加载图像”按钮,在button1 Click事件句柄中添加以下代码:

  1: codecs = new RasterCodecs();

  2: //加载图像

  3: image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\IMAGE25pxp"));

  4: //显示图像

  5: imageViewer = new RasterImageViewer();

  6: imageViewer.BackColor = Color.DarkCyan;

  7: imageViewer.Dock = DockStyle.Fill;

  8: imageViewer.InteractiveMode = RasterViewerInteractiveMode.Pan;

  9: imageViewer.HorizontalAlignMode = RasterPaintAlignMode.Center;

 10: imageViewer.VerticalAlignMode = RasterPaintAlignMode.Center;

 11: imageViewer.AutoResetScaleFactor = false;

 12: panelImage.Controls.Add(imageViewer);

 13: imageViewer.BringToFront();

 14: imageViewer.Image = image;

7、双击“设定窗位并保存图像”按钮,在button2 Click事件句柄中添加以下代码:

  1:             if (image == null)

  2:             {

  3:                 MessageBox.Show("请首先加载图像!");

  4:                 return;

  5:             }

  6:             // 将图像转换为16位灰度图像

  7:             GrayscaleCommand grayscaleCmd = new GrayscaleCommand(16);

  8:             grayscaleCmd.Run(image);

  9:

 10:             MinMaxBitsCommand minMaxBitsCmd = new MinMaxBitsCommand();

 11:             minMaxBitsCmd.Run(image);

 12:

 13:             MinMaxValuesCommand minMaxValuesCmd = new MinMaxValuesCommand();

 14:             minMaxValuesCmd.Run(image);

 15:

 16:             int lowBit = minMaxBitsCmd.MinimumBit;

 17:             int highBit = minMaxBitsCmd.MaximumBit;

 18:

 19:             int size = (1 << (image.HighBit - image.LowBit + 1));

 20:             RasterColor[] palette = new RasterColor[size];

 21:

 22:             // 用红色填充LUT的前半部分

 23:             for (int x = 0; x < size / 2; x++)

 24:             {

 25:                 palette[x].R = 255;

 26:                 palette[x].G = 0;

 27:                 palette[x].B = 0;

 28:                 palette[x].Reserved = 0;

 29:             }

 30:

 31:             int minVal = minMaxValuesCmd.MinimumValue;

 32:             int maxVal = minMaxValuesCmd.MaximumValue;

 33:

 34:             //用灰度值填充后半部分

 35:             for (int x = (size / 2); x < size; x++)

 36:             {

 37:                 palette[x].R = Convert.ToByte(Math.Min(255, ((x - minVal) * 255 / (maxVal - minVal))));

 38:                 palette[x].G = palette[x].R;

 39:                 palette[x].B = palette[x].R;

 40:                 palette[x].Reserved = 0;

 41:             }

 42:             image.WindowLevel(lowBit, highBit, palette, RasterWindowLevelMode.PaintAndProcessing);

 43:

 44:             codecs.Save(image, Path.Combine(Application.StartupPath, @"..\..\Pic\WindowLevelResult.bmp"), RasterImageFormat.Bmp, 0);

8、编译、运行程序。结果如下图所示:

LeadTools在图像中设定窗位的具体方法

LeadTools在图像中设定窗位的具体方法

猜你喜欢