将VB转换为C#(字节筛选,颜色) [英] Convert VB to C# (byte sift, color )

查看:99
本文介绍了将VB转换为C#(字节筛选,颜色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我不知道如何转换此代码vb到c#,我不知道如何在c#中用十六进制数分配c#中的颜色常数。有人
可以帮我转换这个vb函数在c#???

Private Function RGB565To32ARGBV3(ByVal rgb16 As UShort) As Int32
        Dim rgb32 As Integer
        Dim tmp As UShort
        rgb32 = &HFF
        tmp = rgb16 >> 11 And &H1F
        tmp = (tmp * &HFF) / &H1F
        rgb32 = (rgb32 << 8) + (tmp And &HFF)
        tmp = rgb16 >> 5 And &H3F
        tmp = (tmp * &HFF) / &H3F
        rgb32 = (rgb32 << 8) + (tmp And &HFF)
        tmp = rgb16 And &H1F
        tmp = (tmp * &HFF) / &H1F
        rgb32 = (rgb32 << 8) + (tmp And &HFF)
        Return rgb32
    End Function




推荐答案

你有很多不同整数的混合类型在这里,所以C#等价物必须有一些强制转换和调整:

You have a lot of mixing of different integer types here, so the C# equivalent has to have a few casts and adjustments:

	public Int32 RGB565To32ARGBV3(ushort rgb16)
	{
		int rgb32 = 0;
		ushort tmp = 0;
		rgb32 = 0xFF;
		tmp = (ushort)(rgb16 >> 11 & 0x1F);
		tmp = Convert.ToUInt16((tmp * 0xFF) / (double)0x1F);
		rgb32 = (rgb32 << 8) + (tmp & 0xFF);
		tmp = (ushort)(rgb16 >> 5 & 0x3F);
		tmp = Convert.ToUInt16((tmp * 0xFF) / (double)0x3F);
		rgb32 = (rgb32 << 8) + (tmp & 0xFF);
		tmp = (ushort)(rgb16 & 0x1F);
		tmp = Convert.ToUInt16((tmp * 0xFF) / (double)0x1F);
		rgb32 = (rgb32 << 8) + (tmp & 0xFF);
		return rgb32;
	}

请注意"双倍"施法是迫使浮点除法的必要条件。  VB"/"运算符总是进行浮点除法(例如,结果可能是浮点类型),但C#将执行整数除法,除非
其中一个操作数是浮点类型。

Note that the "double" casts are necessary to force floating point division.  The VB "/" operator always does floating point division (e.g., the result could be a floating point type), but C# will perform integer division unless one of the operands is of a floating point type.

但是,你然后将结果分配给整数类型,那么你需要再次调整...

However, you're then assigning the result to an integer type, so then you need to adjust again...

所以你可能会发现你可以简化这个。

So you might find that you can simplify this.


这篇关于将VB转换为C#(字节筛选,颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆