gpt4 book ai didi

c# - 在 C# 中使用 twain 将图像作为图像类获取

转载 作者:行者123 更新时间:2023-11-30 15:07:56 24 4
gpt4 key购买 nike

我可以使用 twaindotnet 连接并从我的设备获取图像.但我想将图像处理为 Image 类。当我尝试这样的事情时:

    ...
ArrayList pics = tw.TransferPictures();
EndingScan();
tw.CloseSrc();

if(pics.Count > 0) {
IntPtr img = (IntPtr) pics[ 0 ];
PicForm newpic = new PicForm( img );
Image r = Image.FromHbitmap(img, this.Handle);
picturebox.Image = r;
}
...

我在行中收到“错误:GDI+ 中发生一般错误”的错误,

Image r = Image.FromHbitmap(img, this.Handle);

那我哪里错了?我怎样才能得到图像作为图像?

最佳答案

我也发现调用 Image.FromHbitmap 是不够的。

我查看了 TwainDotNet你提到的图书馆,我在那里找到了 BitmapRenderer类(class)。

仅提取相关位,就可以很容易地使用它来创建一个简单的静态方法,您可以传入从 TWAIN 获得的 IntPtr(在您的情况下是您的 img 变量)并转换它到Bitmap .因此,您可以这样调用它:

Image r = TwainBitmapConvertor.ToBitmap(img);

这是代码(它只适用于 x86 并且需要整理但它完成了工作):

public static class TwainBitmapConvertor
{
[StructLayout(LayoutKind.Sequential, Pack = 2)]
private class BitmapInfoHeader
{
public int Size;
public int Width;
public int Height;
public short Planes;
public short BitCount;
public int Compression;
public int SizeImage;
public int XPelsPerMeter;
public int YPelsPerMeter;
public int ClrUsed;
public int ClrImportant;
}

[DllImport("gdi32.dll", ExactSpelling = true)]
private static extern int SetDIBitsToDevice(IntPtr hdc,
int xdst, int ydst, int width, int height, int xsrc,
int ysrc, int start, int lines, IntPtr bitsptr,
IntPtr bmiptr, int color);

[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GlobalLock(IntPtr handle);

[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern bool GlobalUnlock(IntPtr handle);

[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GlobalFree(IntPtr handle);

public static Bitmap ToBitmap(IntPtr dibHandle)
{
var bitmapPointer = GlobalLock(dibHandle);

var bitmapInfo = new BitmapInfoHeader();
Marshal.PtrToStructure(bitmapPointer, bitmapInfo);

var rectangle = new Rectangle();
rectangle.X = rectangle.Y = 0;
rectangle.Width = bitmapInfo.Width;
rectangle.Height = bitmapInfo.Height;

if (bitmapInfo.SizeImage == 0)
{
bitmapInfo.SizeImage =
((((bitmapInfo.Width * bitmapInfo.BitCount) + 31) & ~31) >> 3)
* bitmapInfo.Height;
}

// The following code only works on x86
Debug.Assert(Marshal.SizeOf(typeof(IntPtr)) == 4);

int pixelInfoPointer = bitmapInfo.ClrUsed;
if ((pixelInfoPointer == 0) && (bitmapInfo.BitCount <= 8))
{
pixelInfoPointer = 1 << bitmapInfo.BitCount;
}

pixelInfoPointer = (pixelInfoPointer * 4) + bitmapInfo.Size
+ bitmapPointer.ToInt32();

IntPtr pixelInfoIntPointer = new IntPtr(pixelInfoPointer);

Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height);

using (Graphics graphics = Graphics.FromImage(bitmap))
{
IntPtr hdc = graphics.GetHdc();

try
{
SetDIBitsToDevice(hdc,
0, 0, rectangle.Width, rectangle.Height, 0, 0, 0,
rectangle.Height, pixelInfoIntPointer, bitmapPointer, 0);
}
finally
{
graphics.ReleaseHdc(hdc);
}
}

bitmap.SetResolution(PpmToDpi(bitmapInfo.XPelsPerMeter),
PpmToDpi(bitmapInfo.YPelsPerMeter));

GlobalUnlock(dibHandle);
GlobalFree(dibHandle);

return bitmap;
}

private static float PpmToDpi(double pixelsPerMeter)
{
double pixelsPerMillimeter = (double)pixelsPerMeter / 1000.0;
double dotsPerInch = pixelsPerMillimeter * 25.4;
return (float)Math.Round(dotsPerInch, 2);
}
}

关于c# - 在 C# 中使用 twain 将图像作为图像类获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6131002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com