s***r 发帖数: 2604 | 1 在用一个third party的dll, 原来是c++写的, 现在wrap好了. 接口反回值应该是个
image, 但现在的三个可见返回值是 short* img, int w, int h
用这三个怎么在c#下把bitmap弄出来呢, 谢谢. 这个short* 是不是可以直接转成
IntPtr用啊, 谢谢 |
s***r 发帖数: 2604 | 2 so, basically without image structure info I can not fetch the image out?
even I already have theaddress, width and height of it?
can u give some marshall related link about this. I tried some marshall.copy
(), but not work. I am worry about something wrong in my code. |
L******e 发帖数: 136 | 3 it is possible to do
Bitmap^ bmp = Bitmap::FromHbitmap((IntPtr)hBmp);
from your short* and size (w,h) you should be able to get IntPtr for the
bitmap.. |
L******e 发帖数: 136 | 4 see this link:
http://stackoverflow.com/questions/2238049/it-is-possible-to-ge
you should be able to do this without unsafe code using GCHandle. Here is a
sample:
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
try
{
IntPtr pointer = handle.AddrOfPinnedObject();
}
finally
{
if (handle.IsAllocated)
{
handle.Free();
}
} |
L******e 发帖数: 136 | |
s***r 发帖数: 2604 | 6 thank u, I will try each of them
【在 L******e 的大作中提到】 : http://msdn.microsoft.com/en-us/library/k061we7x.aspx : Image.FromHbitmap Method (IntPtr)
|
s***r 发帖数: 2604 | 7 how should I get the "array" in the example code. what is the way to read it
from memory? Something like this?
short* img;
int w;
int h;
Byte[] barray = new Byte[w*h*3];
Marshal.copy(IntPtr(img), barray, 0, w*h*3);
a
【在 L******e 的大作中提到】 : see this link: : http://stackoverflow.com/questions/2238049/it-is-possible-to-ge : you should be able to do this without unsafe code using GCHandle. Here is a : sample: : GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned); : try : { : IntPtr pointer = handle.AddrOfPinnedObject(); : } : finally
|
s***r 发帖数: 2604 | 8 this dose no work
here basically is the code:
Byte[w*h] barray = new byte[w*h];
Mashal.copy((intptr)(img), barray, 0,w*h);
GChandle hd = GChandle.Alloc(barray, Gchandletype.Pinned);
try
{IntPtr p = hd.AddrofPinnedObject();
Bitmap b = Bitmap.FromHbitmap(p);}
finally
{;}
the line bitmap.fromhbitmap(p), will throw an error
it
【在 s***r 的大作中提到】 : how should I get the "array" in the example code. what is the way to read it : from memory? Something like this? : short* img; : int w; : int h; : Byte[] barray = new Byte[w*h*3]; : Marshal.copy(IntPtr(img), barray, 0, w*h*3); : : a
|
L******e 发帖数: 136 | 9 You need to get IntPtr from that short* right? did you try the code below
the one I quoted, in the same stackoverflow link?
unsafe
{
fixed (int* pArray = array)
{
IntPtr intPtr = new IntPtr((void *) pArray);
}
}
once you get this IntPtr you can do the rest I think.. |
s***r 发帖数: 2604 | 10 Yes, I did with following code
Byte[w*h] barray = new byte[w*h]; Mashal.copy((intptr)(img), barray, 0,w*h);
unsafe{fixed(byte* parray=barray)
{InrPtr p= new IntPtr((void* parray));
Bitmap bmp = Bitmap.FromHbitmap(p);}}
same error, on lastline give me "A generic error occured in GDI+"
Is there anything wrong with the code?
do u think the size w,h matters, cz in the run time they are 1066,782 which
is weird to me.
thank u.
在 Leyunque (Le yunque) 的大作中提到: 】 |
L******e 发帖数: 136 | 11 it is a short[w x h] array, why do you need to convert it to byte? |
s***r 发帖数: 2604 | 12 I tried short, byte and int, none of them work
【在 L******e 的大作中提到】 : it is a short[w x h] array, why do you need to convert it to byte?
|