Windows Develop Bookmark and Share   
 index > Windows Forms General > Help fixing my insert inmage control!
 

Help fixing my insert inmage control!

My inserting image code doesn't give me any errors, but when I attempt to use it, it will highlight Bitmap myBitmap = new Bitmap (1strFile);

And it will crash my program. Could I please have help fixing this code so it can open any type of image?

Also, how do I make it so that images are able to be resized?

Code Snippet

private void insertImageToolStripMenuItem_Click(object sender, EventArgs e)

{

string lstrFile = openFileDialog1.FileName;

Bitmap myBitmap = new Bitmap(lstrFile);

// Copy the bitmap to the clipboard.

Clipboard.SetDataObject(myBitmap);

// Get the format for the object type.

DataFormats.Format myFormat = DataFormats.GetFormat (DataFormats.Bitmap);

// After verifying that the data can be pasted, paste

if (txtContent.CanPaste(myFormat))

{

txtContent.Paste(myFormat);

}

else

{

MessageBox.Show("The data format that you attempted site" +

" is not supportedby this control.");

}

}

TheTrueFace  Wednesday, April 23, 2008 2:50 AM

Hi TheTrueFace,

The Bitmap is inherited from Image Class, so it can only accept the image file. I would suggest you put the code into a try catch block to avoid your application from crashing. It would be better if you also filter the file in the OpenFileDialog. You can try something like the following:

Code Snippet

private void btnInsertImage_Click(object sender, EventArgs e)

{

openFileDialog1.Title = "Insert Image File";

openFileDialog1.Filter = "Bitmap Files|*.bmp|JPEG Files|*.jpg|GIF Files|*.gif";

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

try

{

string strImagePath = openFileDialog1.FileName;

Bitmap img = new Bitmap(strImagePath);

Clipboard.SetDataObject(img);

DataFormats.Format df;

df = DataFormats.GetFormat(DataFormats.Bitmap);

if (this.richTextBox1.CanPaste(df))

{

this.richTextBox1.Paste(df);

}

}

catch

{

MessageBox.Show("Unable to insert image format selected.");

}

}

}

Whit the above code, you can resize the image by dragging the edge of the image.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, April 29, 2008 9:12 AM

I have a different code now, it allows me to resize and functions perfectly. The only problem is that when I go over top of it to resize it my cursor is still the text style. How do I change it so that when I go overtop of an image the cursor changes?

Code Snippet

private void insertImageToolStripMenuItem_Click(object sender, EventArgs e)

{

dlgOpen.Title = "RTE - Insert Image File";

dlgOpen.DefaultExt = "rtf";

dlgOpen.Filter = "Bitmap Files|*.bmp|JPEG Files|*.jpg|GIF Files|*.gif";

dlgOpen.FilterIndex = 1;

dlgOpen.ShowDialog();

if (dlgOpen.FileName == "")

{

return;

}

try

{

string strImagePath = dlgOpen.FileName;

Image img;

img = Image.FromFile(strImagePath);

Clipboard.SetDataObject(img);

DataFormats.Format df;

df = DataFormats.GetFormat(DataFormats.Bitmap);

if (this.txtContent.CanPaste(df))

{

this.txtContent.Paste(df);

}

}

catch (Exception)

{

MessageBox.Show("Unable to insert image format selected.", "RTE - Paste", MessageBoxButtons.OK,

MessageBoxIcon.Error);

}

}

TheTrueFace  Wednesday, April 30, 2008 6:32 AM

You can use google to search for other answers

Custom Search

More Threads

• Compact & Repair Database
• Grabbing data from a control on a window
• Issue with resource file
• Focus problem on C# Winform
• mouseover event and listbox control
• Changing listview cells backcolor...
• How do I update/refresh a .Count in the txtCount ...???
• Connecting and Uploading To FTP
• auto scrolling in grid
• DateTimePicker focus on the day part