Pro*_*oob 3 c# vb.net pointers
为了更好地学习vb.net和C#,我正在进行一个项目(在SourceForge上找到TreeViewAdv)并尝试将其代码转换为VB.虽然我希望能够手动转换代码(因为这是一个学习项目),但我目前正在使用C#到VB代码转换器(在www.DeveloperFusion.com上找到)来推动滚动基本了解第一.我转换的代码大多没有问题,但有一个问题(可能是2).见代码:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace Aga.Controls
{
public static class BitmapHelper
{
[StructLayout(LayoutKind.Sequential)]
private struct PixelData
{
public byte B;
public byte G;
public byte R;
public byte A;
}
public static void SetAlphaChanelValue(Bitmap image, byte value)
{
if (image == null)
throw new ArgumentNullException("image");
if (image.PixelFormat != PixelFormat.Format32bppArgb)
throw new ArgumentException("Wrong PixelFormat");
BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
PixelData* pPixel = (PixelData*)bitmapData.Scan0;
for (int i = 0; i < bitmapData.Height; i++)
{
for (int j = 0; j < bitmapData.Width; j++)
{
pPixel->A = value;
pPixel++;
}
pPixel += bitmapData.Stride - (bitmapData.Width * 4);
}
}
image.UnlockBits(bitmapData);
}
}
}
Run Code Online (Sandbox Code Playgroud)
VB.Net(转换后)
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Namespace Aga.Controls
Public NotInheritable Class BitmapHelper
Private Sub New()
End Sub
<StructLayout(LayoutKind.Sequential)> _
Private Structure PixelData
Public B As Byte
Public G As Byte
Public R As Byte
Public A As Byte
End Structure
Public Shared Sub SetAlphaChanelValue(ByVal image As Bitmap, ByVal value As Byte)
If image Is Nothing Then
Throw New ArgumentNullException("image")
End If
If image.PixelFormat <> PixelFormat.Format32bppArgb Then
Throw New ArgumentException("Wrong PixelFormat")
End If
Dim bitmapData As BitmapData = image.LockBits(New Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)
Dim pPixel As Pointer(Of PixelData) = DirectCast(bitmapData.Scan0, Pointer(Of PixelData))
For i As Integer = 0 To bitmapData.Height - 1
For j As Integer = 0 To bitmapData.Width - 1
pPixel.A = value
pPixel += 1
Next
pPixel += bitmapData.Stride - (bitmapData.Width * 4)
Next
image.UnlockBits(bitmapData)
End Sub
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
我相信,问题中的代码行是: C#
PixelData* pPixel = (PixelData*)bitmapData.Scan0;
Run Code Online (Sandbox Code Playgroud)
在VB中转换为此
Dim pPixel As Pointer(Of PixelData) = DirectCast(bitmapData.Scan0, Pointer(Of PixelData))
Run Code Online (Sandbox Code Playgroud)
Intellisense告诉我指针(Of PixelData)有问题
我的问题: 让上面的VB代码像C#代码一样运行的最佳方法是什么(结果而不是方法论),以及解决方案的工作原理(我希望理解其中的原因)?
在回答或评论时应该记住的事项:
1)我已经知道CRL或托管编程语言不使用指针
2)我不是想在vb.net中找到一种使用指针或不安全代码的方法我知道这不可能.
3)我不会抛弃VB,也不希望简单地将C#代码保存在单独的程序集中并从VB引用它.我知道它有局限性,但这是一个LEARNING项目,所以当我去面试并问他时,"你能用VB做X 吗?" 我可以自信地说是的.
4)同样,这里最重要的不是我走哪条路而是目的地.我知道通过C#到达目的地的道路较短,但我希望通过VB.
对于奖励积分,我期待声明
pPixel + = 1
不能在VB端工作(因为pPixel是一个结构,见上文),但它在C#端构建良好.为什么这(工作/不工作)在各自的语言中.这不是关于安全/不安全代码块的问题,就像代码ACTUALLY RUNS并且由于不适当的转换/类型使用(int 1 - > struct pPixel)而引发错误时,为什么?
在VB.NET中没有指针支持,您需要回退到框架支持功能.Marshal.WriteByte适合票证:
Dim pPixel = bitmapData.Scan0
For y As Integer = 0 To bitmapData.Height - 1
For x As Integer = 0 To bitmapData.Width - 1
Marshal.WriteByte(pPixel, 4 * x + 3, value)
Next
pPixel += bitmapData.Stride
Next
Run Code Online (Sandbox Code Playgroud)
当然,永远不要忽视.NET支持的优秀语言互操作.VB.NET程序可以非常轻松地使用C#类库项目.
| 归档时间: |
|
| 查看次数: |
2057 次 |
| 最近记录: |