Note that there are some explanatory texts on larger screens.

plurals
  1. POPictureBox always throws NullReferenceException
    primarykey
    data
    text
    <p>A simple code that should show an image using PictureBox doesn't work (frm is my form):</p> <pre><code>PictureBox pb = new PictureBox(); pb.Image = new Bitmap("1.jpg"); pb.SizeMode = PictureBoxSizeMode.Zoom; frm.Controls.Add(pb); </code></pre> <p>When event with this code happens I have NullReferenceExcpetion</p> <p>The error occurs at <code>frm.Controls.Add(pb)</code></p> <p>The exception is: </p> <blockquote> <p>System.NullReferenceException: Object reference not set to an instance of an object. at Form1.HotKeyManager_HotKeyPressed(Object sender, HotKeyEventArgs e) in C:\Users\Алексей\Documents\Visual Studio 2010\Projects\NotepadCSharpSetup\WinFormsAgain\RealTrayForm\Test.cs:line 52</p> </blockquote> <p>Full code : </p> <pre><code>static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) { Size ScreenSize = Screen.PrimaryScreen.Bounds.Size; Bitmap image = new Bitmap(ScreenSize.Width, ScreenSize.Height); using (Graphics g = Graphics.FromImage(image)) { g.CopyFromScreen(Point.Empty, Point.Empty, ScreenSize); } Bitmap preview = new Bitmap(image.Width / 10, image.Height / 10); using (Graphics gr = Graphics.FromImage(preview)) { gr.SmoothingMode = SmoothingMode.AntiAlias; gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = PixelOffsetMode.HighQuality; gr.DrawImage(image, new Rectangle(0, 0, image.Width / 10, image.Height / 10)); } preview.Save("1.jpg"); Form frm = (Form)sender; PictureBox pb = new PictureBox(); pb.Image = new Bitmap("1.jpg"); pb.SizeMode = PictureBoxSizeMode.Zoom; frm.Controls.Add(pb); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload