本文介绍了使用元帅或其他方式在VB中使用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以帮助我将这段代码转换为Visual Basic,尤其是那些指针声明吗?请帮忙
Can some body help me to convert this code into visual basic especially those pointers declaration. please help
private unsafe void PutImage(ref System.IntPtr pPtr, int imageBufferSize, ADVANTAGELib.AdvImageType imageType)
{
byte** ppResBuffer = (byte**)pPtr;
byte* pResBuffer = *ppResBuffer;
int lPictureWidth = this.signatureImagePictureBox.Width;
int lPictureHeight = this.signatureImagePictureBox.Height;
using (MemoryStream imageStream = new MemoryStream(imageBufferSize))
{
using (BinaryWriter w = new BinaryWriter(imageStream))
{
for (int i = 0; i < imageBufferSize; i++)
{
w.Write((byte)pResBuffer[i]);
}
Bitmap lPicture = new Bitmap(Image.FromStream(imageStream), lPictureWidth, lPictureHeight);
if (imageType == ADVANTAGELib.AdvImageType.AdvSignatureImage)
{
this.signatureImagePictureBox.Image = lPicture;
}
else
{
this.cardholderImagePictureBox.Image = lPicture;
}
System.Runtime.InteropServices.Marshal.FreeCoTaskMem((System.IntPtr)pResBuffer);
}
}
System.GC.Collect();
}
推荐答案
private void PutImage(IntPtr pPtr, int imageBufferSize, AdvImageType imageType)
{
byte[] data = new byte[imageBufferSize];
Marshal.Copy(pPtr, data, 0, imageBufferSize);
using (MemoryStream imageStream = new MemoryStream(imageBufferSize))
{
using (BinaryWriter w = new BinaryWriter(imageStream))
{
for (int i = 0; i < imageBufferSize; i++)
w.Write(data[i]);
Bitmap lPicture = new Bitmap(
Image.FromStream(imageStream),
signatureImagePictureBox.Width,
signatureImagePictureBox.Height);
if (imageType == AdvImageType.AdvSignatureImage)
signatureImagePictureBox.Image = lPicture;
else
cardholderImagePictureBox.Image = lPicture;
Marshal.FreeCoTaskMem(pPtr);
}
}
}
我已经通过删除ref更改了第一个参数.在这种情况下,将指针传递给指针似乎毫无意义.我还使用了Marshal类从数据创建托管数组,因为那样就无需使用不安全的代码!
当然未经测试,但可以编译,应该可以.
删除了不再需要的不安全关键字:doh!
I have changed the first parameter by removing the ref. There seems little point in passing a pointer to a pointer in this scenario. I have also used the Marshal class to create a managed array from the data instead as then there is no need to use unsafe code!
Untested of course, but it compiles and should be OK.
Removed the unsafe keyword as no longer needed :doh!
这篇关于使用元帅或其他方式在VB中使用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!