公司新产品需计算一个图象(来自数码相机)中指定颜色的面积,先用VB6做过,用C#找新办法来实现,但结果却不同,实验证明后者错了。
1、用画图程序做了 100 X 100 的图形,将其等分为两部分,一部分填充为黑色。2、用VB6调用 API函数——GetBitmapBits ,经转换后得到颜色列表,统计 vbWhite 颜色的象素数量为5000——正确。3、打开 M$ C# 2005,将 PictureBox.Image 的流数据 Save 到 MemoryStream 中,循环MemoryStream,将字节数据组合为颜色值,比较累加后,得结果值4868。各方面问题都跟踪 Debug 过了,不知怎么回事。难道2005年的新技术还比不上98年的VB6!月底必须交货,高手救命啊!相关C#源程序如下:private int GetColorLists(Image SrcPic,Color PickColor)
{
//传递一个图像对象(image)和颜色数组,返回指定颜色(PickColor)的象素数
Stream Saa = new MemoryStream();
SrcPic.Save(Saa, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] Pdd = new byte[Saa.Length];
Saa.Read(Pdd, 0, (int)Saa.Length);//流数据 to byte[] int Cr = 0, Cc = 0;
int CrCount = 0;
for (int Rr = 0; Rr < Pdd.Length - 3; Rr += 3)
{
if (Cc == SrcPic.Width)
{
Rr += 2;//分行时,有两个字节=255,经查无用
Cr++;
Cc = 0;
}
Color Gcolor = Color.FromArgb(Pdd[Rr + 2], Pdd[Rr + 1], Pdd[Rr]);//得颜色
if (ColorTranslator.ToOle(Gcolor ) == ColorTranslator.ToOle(PickColor)) CrCount++;//比较累加
Cc++;
Application.DoEvents();
}
return CrCount;
} 另外,别告诉我在C#里也能用GetBitmapBits,之所以弃VS98而用 Dot net,就是想用新工提供的新方法,若非万不得已,我不打算穿新鞋走老路,若一定要把C#当VB6用,若.net解决问题还得老用非托管 API,我还不如用VB6来得轻快。
1、用画图程序做了 100 X 100 的图形,将其等分为两部分,一部分填充为黑色。2、用VB6调用 API函数——GetBitmapBits ,经转换后得到颜色列表,统计 vbWhite 颜色的象素数量为5000——正确。3、打开 M$ C# 2005,将 PictureBox.Image 的流数据 Save 到 MemoryStream 中,循环MemoryStream,将字节数据组合为颜色值,比较累加后,得结果值4868。各方面问题都跟踪 Debug 过了,不知怎么回事。难道2005年的新技术还比不上98年的VB6!月底必须交货,高手救命啊!相关C#源程序如下:private int GetColorLists(Image SrcPic,Color PickColor)
{
//传递一个图像对象(image)和颜色数组,返回指定颜色(PickColor)的象素数
Stream Saa = new MemoryStream();
SrcPic.Save(Saa, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] Pdd = new byte[Saa.Length];
Saa.Read(Pdd, 0, (int)Saa.Length);//流数据 to byte[] int Cr = 0, Cc = 0;
int CrCount = 0;
for (int Rr = 0; Rr < Pdd.Length - 3; Rr += 3)
{
if (Cc == SrcPic.Width)
{
Rr += 2;//分行时,有两个字节=255,经查无用
Cr++;
Cc = 0;
}
Color Gcolor = Color.FromArgb(Pdd[Rr + 2], Pdd[Rr + 1], Pdd[Rr]);//得颜色
if (ColorTranslator.ToOle(Gcolor ) == ColorTranslator.ToOle(PickColor)) CrCount++;//比较累加
Cc++;
Application.DoEvents();
}
return CrCount;
} 另外,别告诉我在C#里也能用GetBitmapBits,之所以弃VS98而用 Dot net,就是想用新工提供的新方法,若非万不得已,我不打算穿新鞋走老路,若一定要把C#当VB6用,若.net解决问题还得老用非托管 API,我还不如用VB6来得轻快。
解决方案 »
- c# 自动更新问题 我改了代码后用户可以联网直接更新
- 菜鸟求.!请GGJJ来解惑.
- 如何在网络中广播式发送文本信息?
- ???关于xml对应到treeview节点的问题!!!
- C#调用外部程序,能一起打包成一个EXE吗?等达人
- c#访问网络缺少程序集 是怎么回事
- 如何在DataGrid里的用substring从数据库的news表的news_BiaoTi(原来长度是30,把新闻标题的前10个字符取出,然后后面加省略号,并显示在D
- 求教一个小问题!!!!急急急
- 请教【创建对象】的问题,谢谢
- 一个关于多表的复杂统计查询的问题?谢谢
- 新手问一个简单的问题
- 使用数组保存数据占用了太多的内存,应该如何解决?
你这种做法第一次见,感觉你绕了一个圈
Image 类 | Image 成员 | System.Drawing 命名空间 | C++ 托管扩展编程
语言
C#C++JScriptVisual Basic全部显示
从指定的文件创建 Image 对象。重载列表
从指定的文件创建 Image 对象。[Visual Basic] Overloads Public Shared Function FromFile(String) As Image
[C#] public static Image FromFile(string);
[C++] public: static Image* FromFile(String*);
[JScript] public static function FromFile(String) : Image;
使用该文件中的嵌入颜色管理信息,从指定的文件创建 Image 对象。[Visual Basic] Overloads Public Shared Function FromFile(String, Boolean) As Image
[C#] public static Image FromFile(string, bool);
[C++] public: static Image* FromFile(String*, bool);
[JScript] public static function FromFile(String, Boolean) : Image;
请参见
Image 类 | Image 成员 | System.Drawing 命名空间 | C++ 托管扩展编程
--------------------------------------------------------------------------------发送有关此主题的意见 © 2001-2002 Microsoft Corporation。保留所有权利。
Bitmap 类 | Bitmap 成员 | System.Drawing 命名空间 | Bitmap 成员(Visual J# 语法) | C++ 托管扩展编程
要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET
语言
C#C++JScriptVisual Basic全部显示
获取此 Bitmap 中指定像素的颜色。[Visual Basic]
Public Function GetPixel( _
ByVal x As Integer, _
ByVal y As Integer _
) As Color[C#]
public Color GetPixel(
int x,
int y
);[C++]
public: Color GetPixel(
int x,
int y
);[JScript]
public function GetPixel(
x : int,
y : int
) : Color;参数
x
要检索的像素的 x 坐标。
y
要检索的像素的 y 坐标。
返回值
Color 结构,它表示指定像素的颜色。示例
[Visual Basic, C#] 下面的示例旨在用于 Windows 窗体,它需要 PaintEventArgs e(这是 Paint 事件处理程序的参数)。此代码获取位图中像素的颜色,然后用该颜色填充一个矩形。[Visual Basic]
Public Sub GetPixel_Example(e As PaintEventArgs)
' Create a Bitmap object from an image file.
Dim myBitmap As New Bitmap("Grapes.jpg")
' Get the color of a pixel within myBitmap.
Dim pixelColor As Color = myBitmap.GetPixel(50, 50)
' Fill a rectangle with pixelColor.
Dim pixelBrush As New SolidBrush(pixelColor)
e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100)
End Sub[C#]
public void GetPixel_Example(PaintEventArgs e)
{
// Create a Bitmap object from an image file.
Bimap myBitmap = new Bitmap("Grapes.jpg");
// Get the color of a pixel within myBitmap.
Color pixelColor = myBitmap.GetPixel(50, 50);
// Fill a rectangle with pixelColor.
SolidBrush pixelBrush = new SolidBrush(pixelColor);
e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
}[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的“语言筛选器”按钮 。要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET请参见
Bitmap 类 | Bitmap 成员 | System.Drawing 命名空间 | Bitmap 成员(Visual J# 语法) | C++ 托管扩展编程
--------------------------------------------------------------------------------发送有关此主题的意见 © 2001-2002 Microsoft Corporation。保留所有权利。