|
WPF: Handling Drag and Drop of Image coming from almost every source |
|
|
|
Scritto da Gene
|
|
sabato 07 marzo 2009 |
|
The following code snipplet let you handle drag and drop of a bitmap image coming from a file or directly from a rich document (such as PDF, Word, Open Office, HTML page,...) into your WPF application.
The following DataFormats are handled:
- DataFormats.Bitmap
- DataFormats.Dib
- DataFormats.FileDrop
(click on "Leggi Tutto..." link to view the souce code) =D
/* ... */
System.Windows.Controls.Image Image = new System.Windows.Controls.Image();
Image.Drop += FileDropHandler;
/* ... */
private void FileDropHandler(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)e.Data.GetData(DataFormats.Bitmap);
Image.Source = _imagebitmap;
_reset();
}
else if (e.Data.GetDataPresent(DataFormats.Dib))
{
System.Drawing.Bitmap bitmap = CreateBitmapFromDib((Stream)e.Data.GetData(DataFormats.Dib));
_imagebitmap = _bitmapToSource(bitmap);
Image.Source = _imagebitmap;
_reset();
}
else if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] fileNames = e.Data.GetData(DataFormats.FileDrop, true) as string[];
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri("file:///" + fileNames[0].Replace("\\", "/"));
bmp.EndInit();
_imagebitmap = bmp;
Image.Source = _imagebitmap;
_reset();
}
e.Handled = true;
}
// the CreateBitmapFromDib function was taken from
// http://www.codeproject.com/KB/GDI-plus/DIBtoBitmap.aspx?
// display=PrintAll&fid=355741&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2227947
private System.Drawing.Bitmap CreateBitmapFromDib(Stream dib)
{
// We create a new Bitmap File in memory.
// This is the easiest way to convert a DIB to Bitmap.
// No PInvoke needed.
BinaryReader reader = new BinaryReader(dib);
int headerSize = reader.ReadInt32();
int pixelSize = (int)dib.Length - headerSize;
int fileSize = 14 + headerSize + pixelSize;
MemoryStream bmp = new MemoryStream(fileSize);
BinaryWriter writer = new BinaryWriter(bmp);
// 1. Write Bitmap File Header:
writer.Write((byte)'B');
writer.Write((byte)'M');
writer.Write(fileSize);
writer.Write((int)0);
writer.Write(14 + headerSize);
// 2. Copy the DIB
dib.Position = 0;
byte[] data = new byte[(int)dib.Length];
dib.Read(data, 0, (int)dib.Length);
writer.Write(data, 0, (int)data.Length);
// 3. Create a new Bitmap from our new stream:
bmp.Position = 0;
return new System.Drawing.Bitmap(bmp);
}
private System.Drawing.Bitmap _bitmapFromSource(BitmapSource bitmapsource)
{
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
// from System.Media.BitmapImage to System.Drawing.Bitmap
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new System.Drawing.Bitmap(outStream);
}
return bitmap;
}
private BitmapSource _bitmapToSource(System.Drawing.Bitmap bitmap)
{
BitmapSource destination;
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
destination = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
destination.Freeze();
return destination;
}
|
|
Ultimo aggiornamento ( sabato 07 marzo 2009 )
|