早上,一来就无聊。哎。今天已经10号了。不知不觉,半年又要过去了,一天天老了。感慨得撞墙。后面的代码关键是这个小部分:
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