HOW TO CAPTURE A GROUPBOX IMAGE TO AN EXCEL WORK SHEET USING VB.NET

Darrell Shotwell 0 Reputation points
2026-10-10T18:46:42.6733333+00:00

This vb.net issue an error code bc30638

Dim BMP As Bitmap(TargetGroupBox.WIDTH,TargetGroupBox.HEIGHT)

            BMP.Save(Images_dir & FileName, Imaging.ImageFormat.Png)

            Dim FULLPATH = (Images_dir & FileName)
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Marcin Policht 110K Reputation points MVP Volunteer Moderator
    2026-10-10T20:43:25.29+00:00

    The error BC30638 occurs because Dim BMP As Bitmap(TargetGroupBox.WIDTH, TargetGroupBox.HEIGHT) is not valid VB.NET syntax for creating a Bitmap object. Use New Bitmap(...) instead.

    To capture a Windows Forms GroupBox as a PNG image, use the following code:

    Dim BMP As New Bitmap(TargetGroupBox.Width, TargetGroupBox.Height)
    
    TargetGroupBox.DrawToBitmap(
        BMP,
        New Rectangle(0, 0, BMP.Width, BMP.Height)
    )
    
    Dim FULLPATH As String = IO.Path.Combine(Images_dir, FileName)
    
    BMP.Save(FULLPATH, Imaging.ImageFormat.Png)
    
    BMP.Dispose()
    

    Make sure you have the following imports:

    Imports System.Drawing
    Imports System.IO
    

    This captures the GroupBox and its visible child controls into a bitmap, then saves it as a PNG file.

    To insert the image into an Excel worksheet, if you are using Excel Interop, you can add the following after saving the image:

    Worksheet.Shapes.AddPicture(
        FULLPATH,
        Microsoft.Office.Core.MsoTriState.msoFalse,
        Microsoft.Office.Core.MsoTriState.msoTrue,
        100, 100, -1, -1
    )
    

    Here, Worksheet is your Excel worksheet object, and 100, 100 specify the image's position in points from the worksheet's upper-left corner. The -1, -1 values preserve the image's original dimensions.

    DrawToBitmap works with Windows Forms controls. If TargetGroupBox is a WPF control rather than a Windows Forms GroupBox, the capture method is different.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.