Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:37:21+00:00 2026-06-15T21:37:21+00:00

This is my code: Public Class Form1 Public TheImage As Image = PictureBox1.BackgroundImage Public

  • 0

This is my code:

Public Class Form1
  Public TheImage As Image = PictureBox1.BackgroundImage
  Public Function AppendBorder(ByVal original As Image, ByVal borderWidth As Integer) As Image
    Dim borderColor As Color = Color.Red
    Dim mypen As New Pen(borderColor, borderWidth * 2)
    Dim newSize As Size = New Size(original.Width + borderWidth * 2, original.Height + borderWidth * 2)
    Dim img As Bitmap = New Bitmap(newSize.Width, newSize.Height)
    Dim g As Graphics = Graphics.FromImage(img)

    ' g.Clear(borderColor)
    g.DrawImage(original, New Point(borderWidth, borderWidth))
    g.DrawRectangle(mypen, 0, 0, newSize.Width, newSize.Height)
    g.Dispose()
    Return img
  End Function

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim OutputImage As Image = AppendBorder(TheImage, 2)
    PictureBox1.BackgroundImage = OutputImage
  End Sub
End Class

There is an actual background image centered inside PictureBox1, which I added in the Designer. But when I debug, I get the error message:

InvalidOperationException was unhandled

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:37:22+00:00Added an answer on June 15, 2026 at 9:37 pm
     Public TheImage As Image = PictureBox1.BackgroundImage
    

    That cannot work. PictureBox1 doesn’t yet have a value when this statement executes, that doesn’t happen until the InitializeComponent() method runs. You probably never heard about that yet, the magic incantation is you typing “Public Sub New”. When you press enter then you’ll see this:

    Public Sub New()
    
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
    
    End Sub
    

    That’s the constructor, a very important part of a .NET class. Note the “Add any initialization” comment that was generated. That’s where you initialize TheImage. To make it look like this:

    Public TheImage As Image
    
    Public Sub New()
        InitializeComponent()
        TheImage = PictureBox1.BackgroundImage
    End Sub
    

    If this is all still mysterious then hit the books to learn more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With this code: public partial class Form1 : Form { private static readonly int
I have class where I draw image. This is code: public class CanvasdrawActivity extends
I have this code: public partial class Form1 : Form { public Form1() {
I have this code: namespace TuyenTk { public partial class Form1 : Form {
I have this code: public class Window extends JFrame { public Window(){ ... JButton
Take a look at this code: public class Test { public static void main(String...
I have this code: public class Area { Texture2D point; Rectangle rect; SpriteBatch _sB;
I am working with NHibernate and I get this code below: public class User
This is my code : public class JJD extends JFrame { /** * */
Try this piece of code - public class WhitespaceTest { public static void main(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.