你是说,它在AutoCAD 64位创建的DWG文件上运行良好?好吧,我们当然希望有这种兼容性…
不过需要注意的是:这些结构并不像它们在图形中那样被打包,因为在Keith的代码中,每个字段都与本机单词边界对齐。我尝试了一个到F#的快速脏端口(我情不自禁),并试图通过固定BinaryReader.ReadBytes返回的字节[]来直接读取结构。这可能不是最有效的方法,并且可能会带来一些具有终结性的问题。顺便说一句,图像格式看起来像是PNG。代码如下:
#nowarn "9"
//Copyright 2009 K.E. Blackie
//This work is based in part from a VBA macro
//initially developed by members of www.vbdesign.net
//it may be used freely and may be modified to suit
//your needs as long as credit is given where it is due
open System.IO
open System.Runtime.InteropServices
[]
type BITMAPINFOHEADER =
val biSize: int32
val biWidth: int32
val biHeight: int32
val biPlanes: int16
val biBitCount: int16
val biCompression: int32
val biSizeImage: int32
val biXPelsperMeter: int32
val biYPelsPerMeter: int32
val biClrUsed: int32
val biClrImportant: int32
[]
type IMGREC =
val bytType: byte
val lngStart: int32
val lngLen: int32
[]
type RGBQUAD =
val rgbBlue: byte
val rgbGreen: byte
val rgbRed: byte
val rgbReserved: byte
let byteToType(reader: BinaryReader) =
let bytes = reader.ReadBytes(Marshal.SizeOf(typeof))
let handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
let theStructure = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof) :?> 'T
handle.Free()
theStructure
let getThumbnail strFile boolRetainBackColor boolSaveToFile strSaveName =
let mutable bmp =
new System.Drawing.Bitmap(
1, 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed )
if File.Exists strFile then
let fs = File.OpenRead strFile
try
use br = new BinaryReader(fs)
fs.Seek(13L, SeekOrigin.Begin) |> ignore
let lngImgLoc = br.ReadInt32()
fs.Seek(int64 lngImgLoc + 17L, SeekOrigin.Begin) |> ignore
let lngCurLoc = lngImgLoc + 17
fs.Seek(int64 lngCurLoc + 3L, SeekOrigin.Begin) |> ignore
let bytCnt = br.ReadByte()
if bytCnt > 1uy then
let intCnt = ref 0
while !intCntignore
let udtHeader = byteToType br
if udtHeader.biBitCount = 8s then
let udtColors =
[|for count = 0 to 255 do
yield byteToType br |]
fs.Seek(int64 udtRec.lngStart, SeekOrigin.Begin) |> ignore
let bytBMPBuff = br.ReadBytes udtRec.lngLen
bmpSystem.Drawing.ColorTranslator.ToOle
let lngColor =
if boolRetainBackColor then lngColor
elif lngColor = System.Drawing.ColorTranslator.ToOle System.Drawing.Color.Black then
System.Drawing.ColorTranslator.ToOle System.Drawing.Color.White
elif lngColor = System.Drawing.ColorTranslator.ToOle System.Drawing.Color.White then
System.Drawing.ColorTranslator.ToOle System.Drawing.Color.Black
else lngColor
bmp.SetPixel(udtHeader.biWidth - x,y, System.Drawing.ColorTranslator.FromOle lngColor)
decr lngCnt
intCnt := int bytCnt
finally
fs.Close()
if boolSaveToFile then
if System.String.IsNullOrEmpty strSaveName then
let fName =
System.String.Concat(
Path.GetTempPath(),
Path.GetFileNameWithoutExtension(Path.GetRandomFileName()),
".bmp" )
bmp.Save fName
else
bmp.Save strSaveName
bmp
嗯......我提供的代码只是为了提取位图预览并让用户看到它,没有别的,其他人可以使用它。还是不是..这真的没什么区别。
嗨,Keith,
不要误会我的意思。您的代码很棒,是提取预览图像的有用示例。如果你没有发布它,这真的会有所不同(甚至更糟)。可能是我自己缺乏经验,我被现场包装问题搞糊涂了,因为这有点出乎我的意料。
非常感谢您的信息和启发!真诚的,托尔斯滕 谢谢基思。
比我升级前使用的简单多了。
不用谢
页:
1
[2]