locked
Unable to cast object of type 'System.Windows.Forms.RichTextBox' to type 'System.Windows.Forms.PictureBox RRS feed

  • Question

  • Hello to all Concerned

    How do solve the error type Unable to cast object of type 'System.Windows.Forms.RichTextBox' to type 'System.Windows.Forms.PictureBox' in Visual Studio Desktop 2012 platform in C#.

    I am attempting to save an existing Jpeg image using picturebox component in an Active Mdichild form

    from the Parent form using a menustrip component,

    The code in C# is as follows:

    private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
            {
               
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Images|*.png;*.bmp;*.jpg;*.tiff";
                System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string ext = System.IO.Path.GetExtension(sfd.FileName);
                    switch (ext)
                    {
                        case ".jpg":
                            format = System.Drawing.Imaging.ImageFormat.Jpeg;
                            break;
                        case ".bmp":
                            format = System.Drawing.Imaging.ImageFormat.Bmp;
                            break;
                    }
                    Form activeChildForm = this.ActiveMdiChild;

                    if (activeChildForm != null)
                    {
                        PictureBox pictureBox1 = (PictureBox)activeChildForm.ActiveControl;
                        if (pictureBox1 != null)
                            pictureBox1.Image.Save(sfd.FileName, format); 
                    }
                    //Error Response from compiler:  Additional information: Unable to cast object of type

                       'System.Windows.Forms.RichTextBox' to type  'System.Windows.Forms.PictureBox'.

                }
            }


    g

    Thursday, June 6, 2013 3:19 PM