WinForms FAQ - PictureBox

Find answers for the most frequently asked questions
Expand All Collapse All

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.

Permalink

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)


[C#]
using System.Runtime.InteropServices;
using System.Reflection;


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;
		}
	}
	CloseClipboard();
}
Permalink

This code snippet shows how you can set your PictureBox’s image to be the image from the clipboard:


[C#]
this.pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
[VB.Net]
Me.pictureBox1.Image = CType(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
Permalink

The following code snippet demonstrates how you can drag and copy an image from one picturebox (Source) another (Target:


[C#]
//In the Form Load
//Set AllowDrop of the Target PictureBox to true as this property cannot be set in the Designer
this.pictureBox2.AllowDrop = true;

//Source PictureBox
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
pictureBox1.DoDragDrop( pictureBox1.Image, DragDropEffects.All );
}

//Target PictureBox
//Drag Drop Effects
private void pictureBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
 {

  if ( e.Data.GetDataPresent( DataFormats.Bitmap ) )
   {
    e.Effect = DragDropEffects.Copy;
   }
  else
   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));  
 }
}

[VB.NET]
’In the Form Load
’Set AllowDrop of the Target PictureBox to true as this property cannot be set in the Designer
Me.pictureBox2.AllowDrop = True
 
’Source PictureBox
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)
End If
End Sub
 
’Target PictureBox
’Drag Drop Effects
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
  Else 
   e.Effect = DragDropEffects.None
  End If
End Sub
 
’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)
End If
End Sub

Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.