Forum Discussion

rminnich's avatar
rminnich
Contributor
12 years ago

conversion between .Net image and picture object?

Is there a way in TC to convert directly from a .Net System.Drawing.Bitmap or Image into a Picture object?  I would like to avoid converting it a pixel at a time or using an intermediary file.

7 Replies

  • aqAnt's avatar
    aqAnt
    SmartBear Alumni (Retired)

    Hello Russell,


    You can perform the conversion with Windows Clipboard. For example:




    function Test()

    {

      var image = dotNET.System_Drawing.Bitmap.zctor("C:\\image.bmp");

      image = dotNET.System_Windows_Interop.Imaging.CreateBitmapSourceFromHBitmap(

        image.GetHbitmap(),

        dotNET.System.IntPtr.Zero,

        dotNET.System_Windows.Int32Rect.Empty,

        dotNET.System_Windows_Media_Imaging.BitmapSizeOptions.FromEmptyOptions()

      );

      dotNET.System_Windows.Clipboard.SetImage(image);

      var picture = Sys.Clipboard;

      Log.Picture(picture); 

    }

  • I am using TC 8.5 and have availability to 8.7.  I do not see the dotNET.System_Windows* and dotNet.System_Drawing asseblies on my system.  I do see other dotNet assemblies.
  • aqAnt's avatar
    aqAnt
    SmartBear Alumni (Retired)

    Hello Russell,


    The sample was created in TestComplete 9, but it works fine in TestComplete 8, too.


    In both cases, you need to configure your project's CLR Bridge settings in the following way:


  • how make change in loaded image by dotNET?  for example drawRectangle....

  • aqAnt's avatar
    aqAnt
    SmartBear Alumni (Retired)


    Hi Andrew,



    Use the .NET Graphics class that provides the method. For example:





    function Test()



      var image = dotNET.System_Drawing.Image.FromFile("C:\\image.bmp"); 

      var image_graphics = dotNET.System_Drawing.Graphics.FromImage(image);

      var black_pen = dotNET.System_Drawing.Pen.zctor(dotNET.System_Drawing.Color.Black);

      image_graphics.DrawRectangle(black_pen, 0, 0, 10, 10); 

      image_graphics.DrawImage(image, dotNET.System_Drawing.Point.zctor(0, 0)); 

     

      var image_bitmap = dotNET.System_Drawing.Bitmap.zctor_10(image); 

      var bitmap_source = dotNET.System_Windows_Interop.Imaging.CreateBitmapSourceFromHBitmap(

        image_bitmap.GetHbitmap(),

        dotNET.System.IntPtr.Zero,

        dotNET.System_Windows.Int32Rect.Empty,

        dotNET.System_Windows_Media_Imaging.BitmapSizeOptions.FromEmptyOptions()

      );

     

      dotNET.System_Windows.Clipboard.SetImage(bitmap_source); 

      var picture = Sys.Clipboard;

      Log.Picture(picture); 

    }


  • Thank!

     but one more thing



    How can I insert screen shot into dotNET Graphics



    like this

     dotNET.System_Windows.Clipboard.SetImage(bitmap_source);

       var picture = Sys.Clipboard;

       Log.Picture(picture);

       Log.Message("first img");

      

     var sbitmap=dotNET.System_Windows.Clipboard.GetImage();

    var img=dotNET.System_Drawing.Image.FromHBitmap(sbitmap);



    I have problem with conversation from   System.Windows.Interop.InteropBitmap
  • aqAnt's avatar
    aqAnt
    SmartBear Alumni (Retired)


    Hi Andrew,



    The conversion was discussed on Stack Overflow. Here is the working solution:





    function Test()



      var bitmap_source = dotNET.System_Windows.Clipboard.GetImage();

      var mstream = dotNET.System_IO.MemoryStream.zctor();

      var encoder = dotNET.System_Windows_Media_Imaging.BmpBitmapEncoder.zctor();

      encoder.Frames.Add(

        dotNET.System_Windows_Media_Imaging.BitmapFrame.Create_5(bitmap_source)

        );

      encoder.Save(mstream);

     

      var image_bitmap = dotNET.System_Drawing.Bitmap.zctor_4(mstream);

      var image_graphics = dotNET.System_Drawing.Graphics.FromImage(image_bitmap);

    }