 |
|
 |
Hi
Thanks Brother,...
In God Protection Code and we so code
Mcandovani
|
|
|
|
 |
|
 |
Hi..i try to use your example DIB to System.Bitmap[^]
but i can't find what's IntPtr pPix.....
You can help me?
|
|
|
|
 |
|
 |
I've tried your code in .NET 3.5 and it works great, but now I've loaded the class in WPF (adding a reference to System.Drawing) and it doesn't work.
method: BitmapFromDIB
line: int status = GdipCreateBitmapFromGdiDib(pDIB, pPix, out pBmp);
returns: status = 18 and pBmp = 0
Any idea what in WPF can cause this code not to work?
Thanks in advance!
|
|
|
|
 |
|
 |
BTW If I add a Windows Form to the WPF appplication and put the class inside that file, it does work (so it's an easy fix, but not a nice one).
|
|
|
|
 |
|
 |
Sorry man, I dont have any idea about WPF conflicts.
|
|
|
|
 |
|
 |
ok, thanks anyways. Also just if someone else has the same problem, a Windows Form with a Picturebox solves de problem. There's no need to load the form (now that's really weird)
|
|
|
|
 |
|
 |
SOLUTION: turned out that you have to initialize manually GDI+ in WPF.
|
|
|
|
 |
|
 |
You don't need PInvoke and native memory if you create a bitmap file in memory. A Bitmap file is a 14 byte file header plus a DIB. So you only have to create the 14 bytes and copy the Dib into a new buffer.
To get the Stream of a DrapDrop action call:
(Stream) e.Data.GetData(DataFormats.Dib)
To convert this dib stream to a bitmap use the following method (I tested the method with Firefox and IE and with dropping JPEG, PNG and GIF files):
private Bitmap CreateBitmapFromDib(Stream dib)
{
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);
writer.Write( (byte) 'B');
writer.Write( (byte) 'M');
writer.Write( fileSize);
writer.Write( (int) 0 );
writer.Write( 14 + headerSize);
dib.Position = 0;
byte[] data = new byte[(int) dib.Length];
dib.Read( data, 0, (int) dib.Length);
writer.Write( data, 0, (int) data.Length);
bmp.Position = 0;
return new Bitmap(bmp);
}
BTW: To enable drag and drop on a PictureBox call:
((Control) pictureBox1).AllowDrop = true;
|
|
|
|
 |
|
 |
Excellent.
Finally see a simple copy and paste code to convert a DIB to Bitmap!
Excellent for drag&drop.
Thank you
|
|
|
|
 |
|
 |
thank you
|
|
|
|
 |
|
 |
Just to clarify, this code doesn't work if the bitmap contains an indexed palette. It took me ages to figure out why some images were coming out corrupted.
Anyhow, if you add these lines:
dib.Position = 32;
int reader = 4 * reader.ReadInt32();
if (paletteSize == 0) {
dib.Position = 14;
int bpp = reader.ReadInt16();
if (bpp < 16)
paletteSize = 4 * (2 << (bpp - 1));
}
Also, do this diff so that the offset includes the BMP header, DIB header & Palette
- writer.Write( 14 + headerSize);
+ writer.Write( 14 + headerSize + paletteSize);
This has worked well for me
Edit: Missed out a set_Position, (wine took me over the Balmer Peak!)
modified on Friday, December 19, 2008 5:51 AM
|
|
|
|
 |
|
 |
Awesome! Thanks so much; palette-enabled DIB has been troubling me for quite a while...
|
|
|
|
 |
|
 |
Thanks a lot!
I modified the stuff a little bit to do the whole thing without copying (as bitmaps can get very big), and included changes from oobayly:
using System;
using System.IO;
namespace Limaki.Drawing {
public class BitmapFromDibStream:Stream {
Stream dib = null;
byte[] header = null;
public BitmapFromDibStream(Stream dib) {
this.dib = dib;
makeHeader();
}
private void makeHeader() {
BinaryReader reader = new BinaryReader(dib);
int headerSize = reader.ReadInt32();
int pixelSize = (int) dib.Length - headerSize;
int fileSize = 14 + headerSize + pixelSize;
MemoryStream bmp = new MemoryStream(14);
BinaryWriter writer = new BinaryWriter(bmp);
dib.Position = 32;
int paletteSize = 4 * reader.ReadInt32();
if ( paletteSize == 0 ) {
dib.Position = 14;
int bpp = reader.ReadInt16();
if ( bpp < 16 )
paletteSize = 4 * ( 2 << ( bpp - 1 ) );
}
writer.Write((byte) 'B');
writer.Write((byte) 'M');
writer.Write(fileSize);
writer.Write((int) 0);
writer.Write(14 + headerSize+paletteSize);
header = bmp.GetBuffer();
writer.Close();
dib.Position = 0;
}
public override int Read ( byte[] buffer, int offset, int count ) {
int dibCount = count;
int dibOffset = offset - 14;
int result = 0;
if (_position<14){
int headerCount = Math.Min(count + (int)_position, 14);
Array.Copy(header, _position, buffer, offset, headerCount);
dibCount -= headerCount;
_position += headerCount;
result = headerCount;
}
if ( _position >= 14 ) {
result += dib.Read(buffer, offset + result, dibCount);
_position = 14 + dib.Position;
}
return (int)result;
}
public override bool CanRead {
get { return true; }
}
public override bool CanSeek {
get { return false; }
}
public override bool CanWrite {
get { return false; }
}
public override void Flush () {
}
public override long Length {
get { return 14 + dib.Length; }
}
private long _position = 0;
public override long Position {
get { return _position; }
set {
_position = value;
if ( _position > 14 )
dib.Position = _position - 14;
}
}
public override long Seek ( long offset, SeekOrigin origin ) {
throw new Exception("The method or operation is not implemented.");
}
public override void SetLength ( long value ) {
throw new Exception("The method or operation is not implemented.");
}
public override void Write ( byte[] buffer, int offset, int count ) {
throw new Exception("The method or operation is not implemented.");
}
}
}
Usage:
Stream stream = dataObject.GetData(DataFormats.Dib) as Stream;
BitmapFromDibStream bmp = new BitmapFromDibStream(stream);
Image image = new Bitmap(bmp);
cheers,
lytico
limada
|
|
|
|
 |
|
 |
hello ; Thanks for the code ;
but I have intpr how can I use it in that Stream stream = dataObject.GetData(DataFormats.Dib) as Stream;
other wise what is the dataformats.dib ?
can I directly include my intpr in Stream stream = dataObject.GetData(myintpr) as Stream;??
regards...
|
|
|
|
 |
|
 |
Awesome code. Yet, simple!
|
|
|
|
 |
|
 |
how to get the pointer to DIBpixels?
modified on Tuesday, April 15, 2008 3:28 AM
|
|
|
|
 |
|
 |
Well in my case, i was getting the pointer to DIPpixel after an images is sucessfully scanned, the driver of the scanning device was creating row bitmap of the image and my application got a pointer to that memory area i.e. pointer to DIBPixel.
Hope this helps.
|
|
|
|
 |
|
|
 |
|
 |
Do you know the return values for GdipCreateBitmapFromGdiDib? I am getting a return value of "2" and can't figure out why GdipCreateBitmapFromGdiDib is not working.
|
|
|
|
 |
|
|
 |