|
|
31.1 How can I place a border around a PictureBox?
|
 |
One solution is to use a panel that has a picturebox placed on it with DockStyle.Fill. This will make the picturebox assume the size of the panel. In addition, set the DockPadding.All property to the width of the desired border. Then in the Panel's OnPaint method, call the baseclass and then paint the desired borders.
Here are both VB and C# projects that illustrate how you might go about this. The derived PicturePanel class has properties that allow you to set the bordersize and color as well as the image that is to be displayed. This sample retrieves the image from an embedded resource. It also uses double buffering to minimize flashing as you resize the control. |
31.2 How can I copy a bitmap from the clipboard to a PictureBox?
|
 |
This code snippet shows how you can set your PictureBox's image to be the image from the clipboard:
|
this.pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
|
Me.pictureBox1.Image = CType(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
|
31.3 How can I copy and paste images/graphs etc from MS Office to a PictureBox?
|
 |
Since .NET uses it's own format that is not compatible with the EnhancedMetafile format you will have to use reflection to achieve this.
(From a posting in the microsoft.public.dotnet.framework.drawing newsgroup)
|
using System.Runtime.InteropServices;
|
public const uint CF_METAFILEPICT = 3;
|
public const uint CF_ENHMETAFILE = 14;
|
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
|
public static extern bool OpenClipboard(IntPtr hWndNewOwner);
|
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
|
public static extern bool CloseClipboard();
|
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
|
public static extern IntPtr GetClipboardData(uint format);
|
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
|
public static extern bool IsClipboardFormatAvailable(uint format);
|
//Pasting into PictureBox
|
if (OpenClipboard(this.Handle))
|
if (IsClipboardFormatAvailable(CF_ENHMETAFILE))
|
IntPtr ptr = GetClipboardData(CF_ENHMETAFILE);
|
if (!ptr.Equals(new IntPtr(0)))
|
Metafile metafile = new Metafile(ptr,true);
|
//Set the Image Property of PictureBox
|
this.pictureBox1.Image = metafile;
|
31.4 How can I drag and drop an image from one PictureBox to another?
|
 |
The following code snippet demonstrates how you can drag and copy an image from one picturebox (Source) another (Target:
|
//Set AllowDrop of the Target PictureBox to true as this property cannot be set in the Designer
|
this.pictureBox2.AllowDrop = true;
|
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
|
if (e.Button == MouseButtons.Left)
|
pictureBox1.DoDragDrop( pictureBox1.Image, DragDropEffects.All );
|
private void pictureBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
|
if ( e.Data.GetDataPresent( DataFormats.Bitmap ) )
|
e.Effect = DragDropEffects.Copy;
|
e.Effect = DragDropEffects.None;
|
//Set the image to be the dragged image.
|
private void pictureBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
|
if ( (e.Data.GetDataPresent(DataFormats.Bitmap)))
|
this.pictureBox1.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
|
'Set AllowDrop of the Target PictureBox to true as this property cannot be set in the Designer
|
Me.pictureBox2.AllowDrop = True
|
Private Sub pictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
|
If e.Button = MouseButtons.Left Then
|
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All)
|
Private Sub pictureBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
|
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
|
e.Effect = DragDropEffects.Copy
|
e.Effect = DragDropEffects.None
|
'Set the image to be the dragged image.
|
Private Sub pictureBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
|
If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
|
Me.pictureBox1.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
|
|
|
|
|