Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can some body help me to convert this code into visual basic especially those pointers declaration. please help

C#
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();
}
Posted
Updated 12-Jul-12 5:46am
v4

I don't know how it works in VB but this is how I'd alter this in C#, which should be code that can be easily converted to VB!
C#
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);
        }
    }
}

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.

Edit: Removed the unsafe keyword as no longer needed :doh!
 
Share this answer
 
v2
Though I do not have the answer I can provide you with some information and options.
First of all, why convert this to VB? Since VB is not as good with unsafe code as C# it would make more sense to keep this code in a C# assembly and reference that in a VB project so you can call this function from VB code.

If you would really want to convert this code to VB I suggest reading the following article: How to do pointers in Visual Basic[^].

Other than that the closest you can get to pointers in VB is probably the IntPtr Structure[^].

A last remark, I can't really figure out what your piece of code is doing, but you might want to consider rewriting that code completely so it doesn't use pointers anymore (or call GC.Collect[^]). Generally you'd never need them in VB.
 
Share this answer
 
Thanks I will try to convent it to a DLL, but i am not good to referencing to DLL files. If i can convert only those declarations and refer to them back, i think it might work but i am struggling
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900