DIB to Bitmap

  • Thread starter Thread starter Giles
  • Start date Start date
G

Giles

Im trying to make an application that will accept an image
that has been dragged from IE and dropped onto my form.
The Drag Drop event code is as follows:


Private Sub DragTarget_DragDrop(ByVal sender As Object,
ByVal e As DragEventArgs) Handles pb.DragDrop,
MyBase.DragDrop

If (e.Data.GetDataPresent("DeviceIndependentBitmap")) Then

Dim item As Object = CType(e.Data.GetData
("DeviceIndependentBitmap"), System.Object)

If (e.Effect = DragDropEffects.Copy Or _
e.Effect = DragDropEffects.Move) Then
Dim ms As IO.MemoryStream = CType(item,
IO.MemoryStream)
Dim bm As Bitmap = Bitmap.FromStream(ms) '***
Dim img As Image = bm
pb.Image = bm

End If

End If

End Sub

when executing nothing past the line marked '*** is
executed, and no error is produced.

Can anyone explane whats going on, and how to get this DIB
to display in a pictureBox (pb in the code above)

Thanks in advance,

Giles
 
Hi Giles,
The Problem is you didn't handle the data format of CF_DIB in clipboard
properly.
You may Drag&Drop the image using this way.
You may find the code used in this example in
http://dnetmaster.net/source/dibtoimage.zip
<code>
private void Form1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
object obj = e.Data.GetData(DataFormats.Dib);
System.IO.MemoryStream stream = obj as System.IO.MemoryStream;
if(stream != null)
{

byte[] img = stream.ToArray();
IntPtr p = Marshal.AllocCoTaskMem(Marshal.SizeOf(img[0])*img.Length);
Marshal.Copy(img,0,p,img.Length);
try
{
pictureBox1.Image = TwainGui.DibToImage.WithStream(p);
}
finally
{
Marshal.FreeCoTaskMem(p);
}
}
}
If you have anything unclear , please be free to let me know!
Thanks for using MSDN Newsgroup!
</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| Content-Class: urn:content-classes:message
| From: "Giles" <[email protected]>
| Sender: "Giles" <[email protected]>
| Subject: DIB to Bitmap
| Date: Tue, 14 Oct 2003 18:04:16 -0700
| Lines: 38
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOSuEExCqDWq1HESIqB0iiXansaBg==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54444
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Im trying to make an application that will accept an image
| that has been dragged from IE and dropped onto my form.
| The Drag Drop event code is as follows:
|
|
| Private Sub DragTarget_DragDrop(ByVal sender As Object,
| ByVal e As DragEventArgs) Handles pb.DragDrop,
| MyBase.DragDrop
|
| If (e.Data.GetDataPresent("DeviceIndependentBitmap")) Then
|
| Dim item As Object = CType(e.Data.GetData
| ("DeviceIndependentBitmap"), System.Object)
|
| If (e.Effect = DragDropEffects.Copy Or _
| e.Effect = DragDropEffects.Move) Then
| Dim ms As IO.MemoryStream = CType(item,
| IO.MemoryStream)
| Dim bm As Bitmap = Bitmap.FromStream(ms) '***
| Dim img As Image = bm
| pb.Image = bm
|
| End If
|
| End If
|
| End Sub
|
| when executing nothing past the line marked '*** is
| executed, and no error is produced.
|
| Can anyone explane whats going on, and how to get this DIB
| to display in a pictureBox (pb in the code above)
|
| Thanks in advance,
|
| Giles
|
|
 
Dear Ying-Shen Yu,

Thank you very much for your post. This works fine. I
would never have worked this out!!

I cant understand why microsoft havnt added a method to
the bitmap class to do this!!

Cheers,

Giles
-----Original Message-----
Hi Giles,
The Problem is you didn't handle the data format of CF_DIB in clipboard
properly.
You may Drag&Drop the image using this way.
You may find the code used in this example in
http://dnetmaster.net/source/dibtoimage.zip
<code>
private void Form1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
object obj = e.Data.GetData(DataFormats.Dib);
System.IO.MemoryStream stream = obj as System.IO.MemoryStream;
if(stream != null)
{

byte[] img = stream.ToArray();
IntPtr p = Marshal.AllocCoTaskMem (Marshal.SizeOf(img[0])*img.Length);
Marshal.Copy(img,0,p,img.Length);
try
{
pictureBox1.Image = TwainGui.DibToImage.WithStream(p);
}
finally
{
Marshal.FreeCoTaskMem(p);
}
}
}
If you have anything unclear , please be free to let me know!
Thanks for using MSDN Newsgroup!
</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| Content-Class: urn:content-classes:message
| From: "Giles" <[email protected]>
| Sender: "Giles" <[email protected]>
| Subject: DIB to Bitmap
| Date: Tue, 14 Oct 2003 18:04:16 -0700
| Lines: 38
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcOSuEExCqDWq1HESIqB0iiXansaBg==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54444
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Im trying to make an application that will accept an image
| that has been dragged from IE and dropped onto my form.
| The Drag Drop event code is as follows:
|
|
| Private Sub DragTarget_DragDrop(ByVal sender As Object,
| ByVal e As DragEventArgs) Handles pb.DragDrop,
| MyBase.DragDrop
|
| If (e.Data.GetDataPresent("DeviceIndependentBitmap")) Then
|
| Dim item As Object = CType(e.Data.GetData
| ("DeviceIndependentBitmap"), System.Object)
|
| If (e.Effect = DragDropEffects.Copy Or _
| e.Effect = DragDropEffects.Move) Then
| Dim ms As IO.MemoryStream = CType(item,
| IO.MemoryStream)
| Dim bm As Bitmap = Bitmap.FromStream(ms) '***
| Dim img As Image = bm
| pb.Image = bm
|
| End If
|
| End If
|
| End Sub
|
| when executing nothing past the line marked '*** is
| executed, and no error is produced.
|
| Can anyone explane whats going on, and how to get this DIB
| to display in a pictureBox (pb in the code above)
|
| Thanks in advance,
|
| Giles
|
|

.
 
Back
Top