none
Pointer in VB.NET? RRS feed

  • Question

  • hi all I trying create Application for the BMP Constructor, I already write the code in C# but I can't translate it into VB.NET, because its require pointer, and as far as i know, there are no pointer function in vb.net @@ so how I can translate this:

    FileStream fs = new FileStream(OJSPathBox.Text, FileMode.Open);
                BinaryReader rdr = new BinaryReader(fs);
    
                int width = 555; // its just for example
                int height = 555; //its just for example
                int bytes = width * height * 2;
                int t = 0; // Offset Chunk data bmp, just for example
                int b;
                Bitmap tmp_bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
                Rectangle rect = new Rectangle(0, 0, tmp_bitmap.Width, tmp_bitmap.Height);
                System.Drawing.Imaging.BitmapData bmpData = tmp_bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, tmp_bitmap.PixelFormat);
                unsafe
                {
                    byte* ptr = (byte*)bmpData.Scan0;
                    for (int i = 0; i < bytes; i++)
                    {
                        fs.Position = t + i;
                        //
                        b = fs.ReadByte();
                        
                        *ptr = Convert.ToByte(b);
                        ptr++;
                        if (width % 2 != 0)
                            if ((i + 1) % (width * 2) == 0 && (i + 1) * 2 % width < width - 1)
                            {
                                ptr += 2;
                            }
                    }
                    //ptr += 2;
                    tmp_bitmap.UnlockBits(bmpData);
                    OJSImageBox.Image = tmp_bitmap;
                    fs.Close();
                    fs.Dispose();
                    rdr.Close();

    ptr declaration is using Pointer and I throught, there are no unsafe in vb.net, how I can translate to vb.net language? is this still possible? thanks


    • Edited by ChronO2Jam Tuesday, February 7, 2012 3:24 PM
    Tuesday, February 7, 2012 3:16 PM

Answers

  • It looks like you're still trying to get the bitmap out of a OJS file.  Why can't you do as I recommended in another thread:

    Marshal.Copy the pixels.  Get rid of all the unsafe stuff and the loops.  You aren't doing anything with the pixels, there is no need to access them individually.

    Tuesday, February 7, 2012 3:42 PM

All replies

  • Hi,

    you can (Marshal-)copy the data to an array, process it and copy it back, or use Marshal.Read/WriteByte to access the BitmapData.

    See: http://www.bobpowell.net/lockingbits.htm

    Regards,

      Thorsten


    Tuesday, February 7, 2012 3:29 PM
  • It looks like you're still trying to get the bitmap out of a OJS file.  Why can't you do as I recommended in another thread:

    Marshal.Copy the pixels.  Get rid of all the unsafe stuff and the loops.  You aren't doing anything with the pixels, there is no need to access them individually.

    Tuesday, February 7, 2012 3:42 PM