CAD Image bitmap handle in .NET
Moderators: SDS, support, admin
CAD Image bitmap handle in .NET
I am really struggling to get a System.Drawing.Bitmap from the bitmap handle returned by the DrawCADtoBitmap method. Now I know that this is not a HBITMAP handle and is in fact a pointer to the raw data, so I tried using the BitmapData class in .NET setting the scan0 to the IntPtr returned by the DrawCADtoBitmap, however after bitmap.UnlockBits the image produced is just black.
Any help as to how to do this, even better an example code in C#, would be great, as I can then ascertain if this product can do what I need. (Is there a dotnet verison soon?)
Cheers
Matt
My code is as follows:
Any help as to how to do this, even better an example code in C#, would be great, as I can then ascertain if this product can do what I need. (Is there a dotnet verison soon?)
Cheers
Matt
My code is as follows:
Code: Select all
CadImage.CADFile = CadImage.CreateCAD(IntPtr.Zero, @<font color="blue">"C:\Installs\sm01.DXF"</font id="blue">);
<b>if</b>(CadImage.CADFile == IntPtr.Zero)
{
string test = "error";
<i><font color="blue">//MessageBox.Show("Error open file:" + dlgOpenDXFFile.FileName);
//ActiveForm.Enabled = true;</font id="blue"></i>
return;
}
FRect fRectExtentsCAD = <b>new</b> FRect();
CadImage.GetExtentsCAD(CadImage.CADFile, ref fRectExtentsCAD);
CadImage.fAbsHeight = fRectExtentsCAD.Top - fRectExtentsCAD.Bottom;
CadImage.fAbsWidth = fRectExtentsCAD.Right - fRectExtentsCAD.Left;
<b>int</b> cnt = CadImage.CADLayerCount(CadImage.CADFile);
DXFData aData = <b>new</b> DXFData();
IntPtr Layer;
<b>long</b> C;
<b>for</b> (<b>int</b> i = <font color="blue">0</font id="blue">; i < cnt; i++)
{
Layer = CadImage.CADLayer(CadImage.CADFile, i, ref aData);
C = aData.Color;
<b>if</b>((aData.Flags & <font color="blue">1</font id="blue">) != <font color="blue">0</font id="blue">)
{
C = C | <font color="blue">0x80000000</font id="blue">;
}
<b>if</b>(aData.Text != null)
{
<i><font color="blue">//MessageBox.Show("["+i+"] "+aData.Text);
//cbLayers.Items.Add(aData.Text); //??</font id="blue"></i>
}
}
<b>if</b>(CadImage.CADFile == IntPtr.Zero) <b>return</b>;
System.Drawing.Image img = <b>new</b> System.Drawing.Bitmap(<font color="blue">500,500</font id="blue">);
Rect fRect = <b>new</b> Rect();
fRect.Left = <font color="blue">0</font id="blue">;
fRect.Top = <font color="blue">0</font id="blue">;
fRect.Right = <font color="blue">500</font id="blue">;
fRect.Bottom = <font color="blue">500</font id="blue">;
fRect.BottomRight = <b>new</b> RPoint(fRect.Right, fRect.Bottom);
fRect.TopLeft = <b>new</b> RPoint(fRect.Left, fRect.Top);
<i><font color="blue">/*if(cbStretch.Checked)
{
DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, g.GetHdc(), ref fRect);
return;
}*/</font id="blue"></i>
<b>if</b>(CadImage.fAbsHeight == -<font color="blue">1</font id="blue">)
{
<b>return</b>;
}
<b>float</b> Koef = CadImage.fAbsHeight / CadImage.fAbsWidth;
fRect.Bottom = (<b>int</b>)Math.Round(fRect.Top + (fRect.Right - fRect.Left) * Koef);
fRect.Left = fRect.Left * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FX;
fRect.Top = fRect.Top * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FY;
fRect.Right = fRect.Right * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FX;;
fRect.Bottom = fRect.Bottom * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FY;
fRect.BottomRight = <b>new</b> RPoint(fRect.Right, fRect.Bottom);
fRect.TopLeft = <b>new</b> RPoint(fRect.Left, fRect.Top);
CadImage.fScaleRect.X = CadImage.fAbsWidth / (fRect.Right - fRect.Left);
CadImage.fScaleRect.Y = CadImage.fAbsHeight / (fRect.Bottom - fRect.Top);
<i><font color="blue">//CadImage.DrawCAD(CadImage.CADFile, g.GetHdc(), ref fRect);</font id="blue"></i>
CADDraw cadDraw = <b>new</b> CADDraw();
cadDraw.Size = Marshal.SizeOf(cadDraw);
cadDraw.R.Left = fRect.Left * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FX;
cadDraw.R.Top = fRect.Top * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FY;
cadDraw.R.Right = fRect.Right * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FX;;
cadDraw.R.Bottom = fRect.Bottom * CadImage.fScale / <font color="blue">100</font id="blue"> + CadImage.FY;
cadDraw.R.BottomRight = <b>new</b> RPoint(fRect.Right, fRect.Bottom);
cadDraw.R.TopLeft = <b>new</b> RPoint(fRect.Left, fRect.Top);
cadDraw.DrawMode = <font color="blue">0</font id="blue">;
System.IntPtr framePointer = CadImage.DrawCADtoBitmap(CadImage.CADFile,ref cadDraw);
Bitmap b = <b>new</b> Bitmap(<font color="blue">500,500</font id="blue">); <i><font color="blue">//this is the bitmap we'll be manipulating.</font id="blue"></i>
BitmapData bmData = b.LockBits(<b>new</b> Rectangle(<font color="blue">0</font id="blue">, <font color="blue">0</font id="blue">, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
bmData.Scan0 = framePointer;
b.UnlockBits(bmData);
b.Save(@<font color="blue">"C:\temp\matt3.bmp"</font id="blue">,ImageFormat.Bmp);
OK to continue this thread in conversation with myself [:D]...
I solved it, in the following way - very simple actually - just make sure you release the Hdc handle or it will be black.
System.Drawing.Bitmap b = new Bitmap(500,500);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.FillRectangle(System.Drawing.Brushes.White,0,0,500,500);
System.IntPtr po = g.GetHdc();
DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, po, ref fRect);
g.ReleaseHdc(po);
//g.DrawLine(System.Drawing.Pens.Red,0,0,50,50);
b.Save(@"C:\temp\output.bmp",ImageFormat.Bmp);
I solved it, in the following way - very simple actually - just make sure you release the Hdc handle or it will be black.
System.Drawing.Bitmap b = new Bitmap(500,500);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.FillRectangle(System.Drawing.Brushes.White,0,0,500,500);
System.IntPtr po = g.GetHdc();
DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, po, ref fRect);
g.ReleaseHdc(po);
//g.DrawLine(System.Drawing.Pens.Red,0,0,50,50);
b.Save(@"C:\temp\output.bmp",ImageFormat.Bmp);
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by matt</i>
<br />OK to continue this thread in conversation with myself [:D]...
I solved it, in the following way - very simple actually - just make sure you release the Hdc handle or it will be black.
System.Drawing.Bitmap b = new Bitmap(500,500);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.FillRectangle(System.Drawing.Brushes.White,0,0,500,500);
System.IntPtr po = g.GetHdc();
DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, po, ref fRect);
g.ReleaseHdc(po);
//g.DrawLine(System.Drawing.Pens.Red,0,0,50,50);
b.Save(@"C:\temp\output.bmp",ImageFormat.Bmp);
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Dear Sir,
sorry for delay with answering - we tried to solve the problem.
Bitmap has BITMAPFILEHEADER
If making Bitmap without BITMAPFILEHEADER is the only decision we can make new function for this.
<br />OK to continue this thread in conversation with myself [:D]...
I solved it, in the following way - very simple actually - just make sure you release the Hdc handle or it will be black.
System.Drawing.Bitmap b = new Bitmap(500,500);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.FillRectangle(System.Drawing.Brushes.White,0,0,500,500);
System.IntPtr po = g.GetHdc();
DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, po, ref fRect);
g.ReleaseHdc(po);
//g.DrawLine(System.Drawing.Pens.Red,0,0,50,50);
b.Save(@"C:\temp\output.bmp",ImageFormat.Bmp);
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Dear Sir,
sorry for delay with answering - we tried to solve the problem.
Bitmap has BITMAPFILEHEADER
If making Bitmap without BITMAPFILEHEADER is the only decision we can make new function for this.
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by matt</i>
<br />OK to continue this thread in conversation with myself [:D]...
I solved it, in the following way - very simple actually - just make sure you release the Hdc handle or it will be black.
System.Drawing.Bitmap b = new Bitmap(500,500);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.FillRectangle(System.Drawing.Brushes.White,0,0,500,500);
System.IntPtr po = g.GetHdc();
DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, po, ref fRect);
g.ReleaseHdc(po);
//g.DrawLine(System.Drawing.Pens.Red,0,0,50,50);
b.Save(@"C:\temp\output.bmp",ImageFormat.Bmp);
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Dear Sir,
sorry for delay with answering - we tried to solve the problem.
Bitmap has BITMAPFILEHEADER
If making Bitmap without BITMAPFILEHEADER is the only decision we can make new function for this.
<br />OK to continue this thread in conversation with myself [:D]...
I solved it, in the following way - very simple actually - just make sure you release the Hdc handle or it will be black.
System.Drawing.Bitmap b = new Bitmap(500,500);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b);
g.FillRectangle(System.Drawing.Brushes.White,0,0,500,500);
System.IntPtr po = g.GetHdc();
DLLWin32Import.DrawCAD(DLLWin32Import.CADFile, po, ref fRect);
g.ReleaseHdc(po);
//g.DrawLine(System.Drawing.Pens.Red,0,0,50,50);
b.Save(@"C:\temp\output.bmp",ImageFormat.Bmp);
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
Dear Sir,
sorry for delay with answering - we tried to solve the problem.
Bitmap has BITMAPFILEHEADER
If making Bitmap without BITMAPFILEHEADER is the only decision we can make new function for this.