en! ~c#真的好简洁。贴段C#到VB的小典型。

早上,一来就无聊。哎cry_smile.gif。今天已经10号了。不知不觉,半年又要过去了,一天天老了cry_smile.gif。感慨得icon18.gif撞墙。后面的代码关键是这个小部分:
                   for(int x=0; x<8; x++, flag = !flag)
                    for(int y=0; y<8; y++, flag = !flag)
                        bitmap.SetPixel(x, y, (flag ? white : black));
用VB要用一大块,可能有好的方法,我没有想到,只用了最笨的:
             For x = 0 To 8 - 1
                    flag = Not flag
                    For y = 0 To 8 - 1
                        flag = Not flag
                        Dim tempColor As Color
                        If flag Then
                            tempColor = Color.White
                        Else
                            tempColor = Color.Black
                        End If
                        bitmap.SetPixel(x, y, tempColor)
                    Next
                Next
具体的功能函数如下:
protected static IntPtr GetHalfToneBrush()
        {
            if (_halfToneBrush == IntPtr.Zero)
            { 
                Bitmap bitmap = new Bitmap(8,8,PixelFormat.Format32bppArgb);

                Color white = Color.FromArgb(255,255,255,255);
                Color black = Color.FromArgb(255,0,0,0);

               bool flag=true;
                // Alternate black and white pixels across all lines
                for(int x=0; x<8; x++, flag = !flag)
                    for(int y=0; y<8; y++, flag = !flag)
                        bitmap.SetPixel(x, y, (flag ? white : black));

                IntPtr hBitmap = bitmap.GetHbitmap();

                Win32.LOGBRUSH brush = new Win32.LOGBRUSH();

                brush.lbStyle = (uint)Win32.BrushStyles.BS_PATTERN;
                brush.lbHatch = (uint)hBitmap;

                _halfToneBrush = Gdi32.CreateBrushIndirect(ref brush);
            }
            return _halfToneBrush;
        }
改成VB:
Protected Shared Function GetHalfToneBrush_() As IntPtr
            If _halfToneBrush.Equals(IntPtr.Zero) Then

                Dim bitmap As New bitmap(8, 8, PixelFormat.Format32bppArgb)

                Dim white As Color = Color.FromArgb(255, 255, 255, 255)
                Dim black As Color = Color.FromArgb(255, 0, 0, 0)

                Dim flag As Boolean = True

                ' Alternate black and white pixels across all lines
                Dim x As Integer
                Dim y As Integer

               For x = 0 To 8 - 1
                    flag = Not flag
                    For y = 0 To 8 - 1
                        flag = Not flag
                        Dim tempColor As Color
                        If flag Then
                            tempColor = Color.White
                        Else
                            tempColor = Color.Black
                        End If
                        bitmap.SetPixel(x, y, tempColor)
                    Next
                Next
                Dim hBitmap As IntPtr = bitmap.GetHbitmap()
                Dim brush As Win32.LOGBRUSH = New Win32.LOGBRUSH
                brush.lbStyle = Convert.ToUInt32(Win32.BrushStyles.BS_PATTERN)
                brush.lbHatch = Convert.ToUInt32(hBitmap)
             _halfToneBrush = Gdi32.CreateBrushIndirect(brush)
            End If
            Return _halfToneBrush
        End Function 'GetHalfToneBrush

转载于:https://www.cnblogs.com/Rimbaud/archive/2004/06/10/14641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值