WIA.ImageFile to System.Drawing.Image

Discussion in 'Microsoft VB .NET' started by Jeremy, Jan 5, 2006.

  1. Jeremy

    Jeremy Guest

    I am creating an imaging service using WIA (Windows Imaging
    Acquisition).

    The problem is that I cannot convert a WIA.ImageFile to a
    System.Drawing.Image to put a timestamp on the image without first
    saving the file using the wia.imagefile.savefile then loading it again
    using System.Drawing.Image.FromFile. This seems pretty ineffecient. I
    have tried coverting it using ctype, but I get an invalid cast
    exception. Is anyone aware of how to accomplish this without saving and
    reloading the file? I appreciate your time...

    Thanks,
    Jeremy

    Dim mydevice As WIA.Device
    Dim manager As New WIA.DeviceManagerClass
    Dim mydialog As New WIA.CommonDialogClass
    Dim strcmdTakePicture As String =
    WIA.CommandID.wiaCommandTakePicture
    Dim strImageformat As String = WIA.FormatID.wiaFormatJPEG
    Dim myItem As WIA.Item
    Dim myImage As WIA.ImageFile
    Dim strFilePath As String = Application.StartupPath &
    "\Images\Dummy.jpg"

    For Each info As WIA.DeviceInfo In manager.DeviceInfos
    'Connect w/o dialog
    If info.DeviceID =
    "{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0000" Then
    mydevice = info.Connect
    End If
    Next

    If IsNothing(mydevice) Then
    mydevice =
    mydialog.ShowSelectDevice(WIA.WiaDeviceType.VideoDeviceType, False,
    True)
    End If

    myItem = mydevice.ExecuteCommand(strcmdTakePicture)

    myImage = CType(myItem.Transfer(strImageformat), WIA.ImageFile)

    'Need to covert to image here instead of SaveFile
    myImage.SaveFile(strFilePath)
     
    Jeremy, Jan 5, 2006
    #1
    1. Advertisements

  2. Hi,

    Maybe this will help

    http://msdn.microsoft.com/coding4fun/someassemblyrequired/lookatme/default.aspx

    Ken
    --------------
    "Jeremy" <> wrote in message
    news:...
    >I am creating an imaging service using WIA (Windows Imaging
    > Acquisition).
    >
    > The problem is that I cannot convert a WIA.ImageFile to a
    > System.Drawing.Image to put a timestamp on the image without first
    > saving the file using the wia.imagefile.savefile then loading it again
    > using System.Drawing.Image.FromFile. This seems pretty ineffecient. I
    > have tried coverting it using ctype, but I get an invalid cast
    > exception. Is anyone aware of how to accomplish this without saving and
    > reloading the file? I appreciate your time...
    >
    > Thanks,
    > Jeremy
    >
    > Dim mydevice As WIA.Device
    > Dim manager As New WIA.DeviceManagerClass
    > Dim mydialog As New WIA.CommonDialogClass
    > Dim strcmdTakePicture As String =
    > WIA.CommandID.wiaCommandTakePicture
    > Dim strImageformat As String = WIA.FormatID.wiaFormatJPEG
    > Dim myItem As WIA.Item
    > Dim myImage As WIA.ImageFile
    > Dim strFilePath As String = Application.StartupPath &
    > "\Images\Dummy.jpg"
    >
    > For Each info As WIA.DeviceInfo In manager.DeviceInfos
    > 'Connect w/o dialog
    > If info.DeviceID =
    > "{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0000" Then
    > mydevice = info.Connect
    > End If
    > Next
    >
    > If IsNothing(mydevice) Then
    > mydevice =
    > mydialog.ShowSelectDevice(WIA.WiaDeviceType.VideoDeviceType, False,
    > True)
    > End If
    >
    > myItem = mydevice.ExecuteCommand(strcmdTakePicture)
    >
    > myImage = CType(myItem.Transfer(strImageformat), WIA.ImageFile)
    >
    > 'Need to covert to image here instead of SaveFile
    > myImage.SaveFile(strFilePath)
    >
     
    Ken Tucker [MVP], Jan 5, 2006
    #2
    1. Advertisements

  3. Jeremy

    Jeremy Guest

    Thanks for your reply Ken. Actually this is one resource I used to
    learn WIA. In the example however, he simply saves the file. I want to
    convert it to a System.Drawing.Image first so I can alter the image
    (add a time stamp). You can not programatically alter a wia.imagefile.
    I was hoping someone could assist me in coverting a wia.imagefile to a
    drawing.image without saving and reloading the file. Dim bmap as Image
    = ctype(MyWiaImage,Image) causes an invalid cast exception.
     
    Jeremy, Jan 5, 2006
    #3
  4. Jeremy

    espenskjervold

    Joined:
    Apr 21, 2009
    Messages:
    1
    Likes Received:
    0
    The solution

    I ran into this issue myself, and googled it to find the solution. I found this thread, which is duplicated on several sites all over the net, but the suggested solution is flawed (the author of the response refers to an article which doesn't cover the issue).

    However, the solution is easy, - the BinaryData()-method does the trick:

    WIA.ImageFile imagefile = item.Transfer(format) as WIA.ImageFile;
    imageBytes = (byte[])imagefile.FileData.get_BinaryData(); // <-- Converts the ImageFile to a byte array

    MemoryStream ms = new MemoryStream(imageBytes);
    Image image = Image.FromStream(ms);
    image.Save("c:/testImage.jpg"); // <-- Converts the byte array to a System.Drawing.Image


    Regards
    Espen S
    http://www.drivstoffguiden.no
     
    espenskjervold, Apr 21, 2009
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Taiwo

    BUG: System.Drawing.Image.FromFile locks file

    Taiwo, Oct 9, 2003, in forum: Microsoft VB .NET
    Replies:
    0
    Views:
    590
    Taiwo
    Oct 9, 2003
  2. Simon Verona
    Replies:
    4
    Views:
    547
    Tim McGarvey
    Feb 26, 2004
  3. Phill.  W

    Drawing.Icon to Drawing.Image, anyone?

    Phill. W, May 24, 2004, in forum: Microsoft VB .NET
    Replies:
    1
    Views:
    251
    Herfried K. Wagner [MVP]
    May 24, 2004
  4. Jeremy Olmstead

    System.IO.FileNotFoundException: imagefile.bmp

    Jeremy Olmstead, Feb 22, 2005, in forum: Microsoft VB .NET
    Replies:
    1
    Views:
    322
    Herfried K. Wagner [MVP]
    Feb 22, 2005
  5. Roberto Hernández

    Windows Image Acquisition (WIA) Taking black photos

    Roberto Hernández, Jul 8, 2006, in forum: Microsoft VB .NET
    Replies:
    3
    Views:
    327
    Peter Huang [MSFT]
    Jul 11, 2006
Loading...

Share This Page