Example Code

BITMAP tools (.NET) Using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This example shows some Vis about BITMAP tools.


Description
You can find some useful Vis can be used in BITMAP applications.

  • Load Bitmap from File
  • Image Pointer Resize Pointer
  • LabVIEW to C#
  • Bitmap application
  • llb


Requirements

  • LabVIEW 2012 (or compatible)


Steps to Implement or Execute Code

  • Select the VIs you pick.
  • Run the program

 

Additional Information or References

VI Snippet

3-342.png

**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
HamidY
Member
Member
on

Hi,

May I ask you how is it possible to use this very useful subvi? How do I open a file and pass the required info (reference) to this subvi. Some documents and example would be very helpful, Thanks Hamid

GriffinRU
Member
Member
on

Simple Example attached.

I will add more tools later.

Thank you for comments,

-Artur

Meiloy
Member
Member
on

Hi!

Thanks for your really helpful tools first.

But I need to pass the bitmap to a c# application. I thought I can read it with vi.GetControlValue("Bitmap"). The only thing i get is an integer number.

Do you know how to pass this bitmap to a c# application?

thanks!

GriffinRU
Member
Member
on

Hi,

How about from Bitmap constructor:

-> GetHbitmap -> IntPtr -> ToInt32 -> C#

-Artur

Meiloy
Member
Member
on

Hi,

can you explain it a bit further, please? I don't really know what to do with that.

thanks,

Meiloy

Meiloy
Member
Member
on

Hi,

can you explain it a bit further, please? I don't really know what to do with that.

thanks,

Meiloy

GriffinRU
Member
Member
on

Example in LabVIEW attached, boxed part need to be programmed in C#. Provided example shows how to get  image from pointer or 1D array.

-Artur

Meiloy
Member
Member
on

I modified the LV to C#.vi and programmed the boxed part in C#. But the pictureBox in C# doesn't show anything.


// I read tmpintptr, width, height, stride with vi.GetControlValue("controlname");

IntPtr intptr = new IntPtr(tmpintptr);

Bitmap bmp = new Bitmap(width,height, stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb ,intptr);

pictureBox1.Image = bmp;

here is the modified vi:

myLV to C#.png

GriffinRU
Member
Member
on

You are unlocking bits too soon...Make sure that you obtain image in C# and then unlockbits.

I.e. Get bitmap and parameters in LabVIEW -> Lock bits -> pass data to C# -> verify data in C# -> Unlock bits in LabVIEW

-Artur

Meiloy
Member
Member
on

I put the bitmap.unlockbits in a case structure and set the "true/false" variable from c#. via vi.Setcontrolvalue("controlname") and then vi.call().

My picturebox still doesn't show anything.

               IntPtr intptr = new IntPtr(tmpintptr);
               Bitmap bmp = new Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, intptr);
               vi.SetControlValue("unlockBits", (int)1);
               vi.Call();

then I pass the bitmap bmp back to the "main" function. But the picturebox still doesn't show anything.

I think it's the newly assigned Intptr, when I call the vi again? But I couldn't find a way to do it in a different way.

here is the vi:

mylvtoc#2.png

Meiloy
Member
Member
on

I could get the image with your Bitmap to Array / Array to Bitmap VIs, https://decibel.ni.com/content/docs/DOC-15159

Thanks! It really helps me out.

-Meiloy

GriffinRU
Member
Member
on

Great,

So, what was different?

In you last posting, I still not sure that it works propertly. I would split example into two vi's one to get Bitmap properties and pointer and second one with unlockbits. Then call first vi and pass output to C# and unlockbits vi...

But, you got it working.

-Artur

Meiloy
Member
Member
on

I think the fault lies with the unlocking and locking in my VI / C# application. I didn't find a proper way to communicate with the VI without really closing it.

I could get it working with your Bitmap to 1D Array VI.

Here is the code I used to make a Bitmap from the passed in Array (byte[]):

//read controls from VI first.


//adapted from: https://decibel.ni.com/content/docs/DOC-15165

Rectangle rect = new Rectangle(0,0,width, height);

Bitmap bmp = new Bitmap(width, height);


System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);



System.Runtime.InteropServices.Marshal.Copy(source, 0, bmpData.Scan0, source.Length);

bmp.UnlockBits(bmpData);


pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

pictureBox1.Image = bmp;

-Meiloy

Here is my adapted VI:

mybmpto1darray.png

andyadams
Member
Member
on

Recently i am testing a .NET barcode program and i am testing how to create barcode image in .NET.  I have some image (.png) it has resolution 96 x 96 and size 100 x 200. It is myImageLoadedFromFile.  And I need to draw properly over it some other images (anotherImage).  I use Photoshop to detect coordinates of X and Y. Photoshop shows some strange coordinates like X = 1.5f and Y= 1,8f when I use mouse to plan where I have to draw that little image. But what I have got is that all images I draw are at the left top corner.  It seems like I have to use different way to know which coordinates I have to use to do it properly.  Any clue how to do it? Which Tool is possible to use to get proper coords. using mouse? 

Here my C# code:

var bitmap  = new Bitmap(myImageLoadedFromFile); 

using (var g = Graphics.FromImage(bitmap))

g.DrawImage(anotherImage, new PointF(1.5f, 1,8f)); 

g.Flush();

}

GriffinRU
Member
Member
on

I am sorry, somehow the was no notifications from ni.forum...

Do you still need a help?

-Artur