本文整理汇总了C#中System.Drawing.Graphics.GetHdc方法的典型用法代码示例。如果您正苦于以下问题:C# Graphics.GetHdc方法的具体用法?C# Graphics.GetHdc怎么用?C# Graphics.GetHdc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Graphics
的用法示例。
在下文中一共展示了Graphics.GetHdc方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BeginRender
internal void BeginRender(Graphics g)
{
if (m_hdc != IntPtr.Zero) throw new Exception("EndRender first.");
m_oG = g;
m_hdc = m_oG.GetHdc();
}
示例2: AllocBuffer
private BufferedGraphics AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
{
if (Interlocked.CompareExchange(ref this.busy, 1, 0) != 0)
{
return this.AllocBufferInTempManager(targetGraphics, targetDC, targetRectangle);
}
this.targetLoc = new Point(targetRectangle.X, targetRectangle.Y);
try
{
Graphics graphics;
if (targetGraphics != null)
{
IntPtr hdc = targetGraphics.GetHdc();
try
{
graphics = this.CreateBuffer(hdc, -this.targetLoc.X, -this.targetLoc.Y, targetRectangle.Width, targetRectangle.Height);
}
finally
{
targetGraphics.ReleaseHdcInternal(hdc);
}
}
else
{
graphics = this.CreateBuffer(targetDC, -this.targetLoc.X, -this.targetLoc.Y, targetRectangle.Width, targetRectangle.Height);
}
this.buffer = new BufferedGraphics(graphics, this, targetGraphics, targetDC, this.targetLoc, this.virtualSize);
}
catch
{
this.busy = 0;
throw;
}
return this.buffer;
}
示例3: Draw
public void Draw(Graphics graphics, Rectangle layoutArea)
{
//Calculate the area to render.
SafeNativeMethods.RECT rectLayoutArea;
rectLayoutArea.Top = (int)(layoutArea.Top * anInch);
rectLayoutArea.Bottom = (int)(layoutArea.Bottom * anInch);
rectLayoutArea.Left = (int)(layoutArea.Left * anInch);
rectLayoutArea.Right = (int)(layoutArea.Right * anInch);
IntPtr hdc = graphics.GetHdc();
SafeNativeMethods.FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = -1; //Indicate character from to character to
fmtRange.chrg.cpMin = 0;
fmtRange.hdc = hdc; //Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc; //Point at printer hDC
fmtRange.rc = rectLayoutArea; //Indicate the area on page to print
fmtRange.rcPage = rectLayoutArea; //Indicate size of page
IntPtr wParam = IntPtr.Zero;
wParam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lParam = IntPtr.Zero;
lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lParam, false);
SafeNativeMethods.SendMessage(this.Handle, SafeNativeMethods.EM_FORMATRANGE, wParam, lParam);
//Free the block of memory allocated
Marshal.FreeCoTaskMem(lParam);
//Release the device context handle obtained by a previous call
graphics.ReleaseHdc(hdc);
}
示例4: Stretch
public static void Stretch(Graphics g, Rectangle rcSrc, Rectangle dest, BitmapData bd)
{
var ar = new byte[Marshal.SizeOf(typeof(BITMAPINFOHEADER))+12];
unsafe {
fixed (byte* par = ar) {
var pInfo = (BITMAPINFOHEADER*)par;
pInfo->Init();
pInfo->biWidth = bd.Width;
pInfo->biHeight = -bd.Height;
pInfo->biPlanes = 1;
pInfo->biBitCount = 32;
pInfo->biCompression = BitmapCompression.BI_BITFIELDS;
pInfo->biSizeImage = (uint)(bd.Stride * bd.Height);
UInt32* masks = (UInt32*)(par + Marshal.SizeOf(typeof(BITMAPINFOHEADER)));
masks[0] = 0xFF0000;
masks[1] = 0xFF00;
masks[2] = 0xFF;
IntPtr hdc = g.GetHdc();
try {
int r = Api.StretchDIBits(hdc, dest.Left, dest.Top, dest.Width, dest.Height,
rcSrc.Left, rcSrc.Top, rcSrc.Width, rcSrc.Height, bd.Scan0, ref *pInfo, 0, RasterOp.SRCCOPY);
Api.Win32Check(Api.GDI_ERROR != r);
} finally {
g.ReleaseHdc(hdc);
}
}
}
}
示例5: DrawRoundRect
public static void DrawRoundRect(Graphics g, int x, int y, int width, int height, RoundRectColors colors) {
IntPtr hdc = g.GetHdc();
const int ROUND_SIZE = 3; //3*3ピクセルは自前で描画
IntPtr pen = Win32.CreatePen(0, 1, colors.border_color);
Win32.SelectObject(hdc, pen);
//上
Win32.MoveToEx(hdc, x + ROUND_SIZE, y);
Win32.LineTo(hdc, x + width - ROUND_SIZE + 1, y);
//下
Win32.MoveToEx(hdc, x + ROUND_SIZE, y + height);
Win32.LineTo(hdc, x + width - ROUND_SIZE + 1, y + height);
//左
Win32.MoveToEx(hdc, x, y + ROUND_SIZE);
Win32.LineTo(hdc, x, y + height - ROUND_SIZE + 1);
//右
Win32.MoveToEx(hdc, x + width, y + ROUND_SIZE);
Win32.LineTo(hdc, x + width, y + height - ROUND_SIZE + 1);
Win32.DeleteObject(pen);
DrawRoundCorner(hdc, x, y, 1, 1, colors); //左上
DrawRoundCorner(hdc, x + width, y, -1, 1, colors); //右上
DrawRoundCorner(hdc, x, y + height, 1, -1, colors); //左下
DrawRoundCorner(hdc, x + width, y + height, -1, -1, colors); //右下
g.ReleaseHdc(hdc);
}
示例6: AlphaLayerControl
public AlphaLayerControl(Graphics parentGraphics, Size size, byte alphaVal)
{
try
{
_alphaImage = new Bitmap(size.Width, size.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
using (Graphics gx = Graphics.FromImage(_alphaImage))
{
IntPtr hdcDst = gx.GetHdc();
IntPtr hdcSrc = parentGraphics.GetHdc();
Win32.BlendFunction blendFunction = new Win32.BlendFunction();
blendFunction.BlendOp = (byte)Win32.BlendOperation.AC_SRC_OVER; // Only supported blend operation
blendFunction.BlendFlags = (byte)Win32.BlendFlags.Zero; // Documentation says put 0 here
blendFunction.SourceConstantAlpha = alphaVal;// Constant alpha factor
blendFunction.AlphaFormat = (byte)0; // Don't look for per pixel alpha
Win32.AlphaBlend(hdcDst, 0, 0, _alphaImage.Width, _alphaImage.Height, hdcSrc, 0, 0, _alphaImage.Width, _alphaImage.Height, blendFunction);
gx.ReleaseHdc(hdcDst); // Required cleanup to GetHdc()
parentGraphics.ReleaseHdc(hdcSrc); // Required cleanup to GetHdc()
}
}
catch
{
GC.Collect();
_alphaImage = new Bitmap(1, 1);
}
}
示例7: AnimateZoomIn
private static void AnimateZoomIn(Graphics formGraphics, IntPtr[] nativeBitmapHandles, Size size)
{
// Create device contexts.
IntPtr targetDC = formGraphics.GetHdc();
IntPtr sourceDC = NativeMethods.GDI32.CreateCompatibleDC(targetDC);
// Capture sourceDC's original empty HBitmap and select first frame for drawing.
IntPtr emptyNativeBitmapHandle = NativeMethods.GDI32.SelectObject(sourceDC, nativeBitmapHandles[0]);
// Draw animation frames to form.
for (int c = 1; c < 90; c++)
{
NativeMethods.BitBlockTransfer(targetDC, 0, 0, size.Width, size.Height, sourceDC, 0, 0);
NativeMethods.GDI32.SelectObject(sourceDC, nativeBitmapHandles[c]);
Thread.Sleep(5);
}
// Draw final frame.
NativeMethods.BitBlockTransfer(targetDC, 0, 0, size.Width, size.Height, sourceDC, 0, 0);
// Clean up.
NativeMethods.GDI32.SelectObject(sourceDC, emptyNativeBitmapHandle);
NativeMethods.GDI32.DeleteDC(sourceDC);
formGraphics.ReleaseHdc(targetDC);
}
示例8: Fill
public static bool Fill(
Graphics gr, Rectangle rc,
Color startColor, Color endColor,
FillDirection fillDir)
{
if (System.Environment.OSVersion.Platform != PlatformID.WinCE)
{
using (SolidBrush br = new SolidBrush(startColor))
{
gr.FillRectangle(br, rc);
}
return true;
}
// 頂点の座標と色を指定
Win32Helper.TRIVERTEX[] tva = new Win32Helper.TRIVERTEX[2];
tva[0] = new Win32Helper.TRIVERTEX(rc.X, rc.Y, startColor);
tva[1] = new Win32Helper.TRIVERTEX(rc.Right, rc.Bottom, endColor);
// どのTRIVERTEXの値を使用するかインデックスを指定
Win32Helper.GRADIENT_RECT[] gra
= new Win32Helper.GRADIENT_RECT[] { new Win32Helper.GRADIENT_RECT(0, 1) };
// GradientFill関数の呼び出し
IntPtr hdc = gr.GetHdc();
bool b = Win32Helper.GradientFill(
hdc, tva, (uint)tva.Length,
gra, (uint)gra.Length, (uint)fillDir);
gr.ReleaseHdc(hdc);
return b;
}
示例9: Fill
// This method wraps the PInvoke to GradientFill.
// Parmeters:
// gr - The Graphics object we are filling
// rc - The rectangle to fill
// startColor - The starting color for the fill
// endColor - The ending color for the fill
// fillDir - The direction to fill
//
// Returns true if the call to GradientFill succeeded; false
// otherwise.
public static bool Fill(
Graphics gr,
Rectangle rc,
Color startColor, Color endColor,
FillDirection fillDir)
{
// Initialize the data to be used in the call to GradientFill.
Win32Helper.TRIVERTEX[] tva = new Win32Helper.TRIVERTEX[2];
tva[0] = new Win32Helper.TRIVERTEX(rc.X, rc.Y, startColor);
tva[1] = new Win32Helper.TRIVERTEX(rc.Right, rc.Bottom, endColor);
Win32Helper.GRADIENT_RECT[] gra = new Win32Helper.GRADIENT_RECT[] {
new Win32Helper.GRADIENT_RECT(0, 1)};
// Get the hDC from the Graphics object.
IntPtr hdc = gr.GetHdc();
// PInvoke to GradientFill.
bool b;
b = Win32Helper.GradientFill(
hdc,
tva,
(uint)tva.Length,
gra,
(uint)gra.Length,
(uint)fillDir);
System.Diagnostics.Debug.Assert(b, string.Format(
"GradientFill failed: {0}",
System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
// Release the hDC from the Graphics object.
gr.ReleaseHdc(hdc);
return b;
}
示例10: GDIMemoryContext
public GDIMemoryContext(Graphics compatibleTo, int width, int height)
{
if (compatibleTo == null || width <= 0 || height <= 0) throw new ArgumentException("Arguments are unacceptable");
IntPtr tmp = compatibleTo.GetHdc();
bool failed = true;
do
{
if ((fDC = NativeMethods.CreateCompatibleDC(tmp)) == IntPtr.Zero) break;
if ((fBitmap = NativeMethods.CreateCompatibleBitmap(tmp, width, height)) == IntPtr.Zero)
{
NativeMethods.DeleteDC(fDC); break;
}
fStockMonoBmp = NativeMethods.SelectObject(fDC, fBitmap);
if (fStockMonoBmp == IntPtr.Zero)
{
NativeMethods.DeleteObject(fBitmap);
NativeMethods.DeleteDC(fDC);
}
else failed = false;
} while (false);
compatibleTo.ReleaseHdc(tmp);
if (failed) throw new SystemException("GDI error occured while creating context");
this.gdiPlusContext = Graphics.FromHdc(this.fDC);
this.fWidth = width; this.fHeight = height;
}
示例11: BitBlt
public static void BitBlt(Graphics g, IntPtr hObject, Rectangle srcRect, Point destPoint)
{
IntPtr pTarget = g.GetHdc();
try
{
IntPtr pSource = Gdi32.CreateCompatibleDC(pTarget);
try
{
IntPtr pOrig = Gdi32.SelectObject(pSource, hObject);
try
{
Gdi32.BitBlt(pTarget, destPoint.X, destPoint.Y, srcRect.Width, srcRect.Height, pSource, srcRect.X, srcRect.Y,
Gdi32.TernaryRasterOperations.SRCCOPY);
}
finally
{
Gdi32.SelectObject(pSource, pOrig);
}
}
finally
{
Gdi32.DeleteDC(pSource);
}
}
finally
{
g.ReleaseHdc(pTarget);
}
}
示例12: DrawRoundRect
public static void DrawRoundRect(Graphics g, int x, int y, int width, int height, RoundRectColors colors)
{
IntPtr hdc = g.GetHdc();
const int ROUND_SIZE = 3; //3*3�s�N�Z���͎��O�ŕ`��
IntPtr pen = Win32.CreatePen(0, 1, colors.border_color);
Win32.SelectObject(hdc, pen);
//��
Win32.MoveToEx(hdc, x + ROUND_SIZE, y);
Win32.LineTo(hdc, x + width - ROUND_SIZE + 1, y);
//��
Win32.MoveToEx(hdc, x + ROUND_SIZE, y + height);
Win32.LineTo(hdc, x + width - ROUND_SIZE + 1, y + height);
//��
Win32.MoveToEx(hdc, x, y + ROUND_SIZE);
Win32.LineTo(hdc, x, y + height - ROUND_SIZE + 1);
//�E
Win32.MoveToEx(hdc, x + width, y + ROUND_SIZE);
Win32.LineTo(hdc, x + width, y + height - ROUND_SIZE + 1);
Win32.DeleteObject(pen);
DrawRoundCorner(hdc, x, y, 1, 1, colors); //����
DrawRoundCorner(hdc, x + width, y, -1, 1, colors); //�E��
DrawRoundCorner(hdc, x, y + height, 1, -1, colors); //����
DrawRoundCorner(hdc, x + width, y + height, -1, -1, colors); //�E��
g.ReleaseHdc(hdc);
}
示例13: MemoryBuffer
public MemoryBuffer(int width, int height)
{
m_bitmap = new Bitmap(width, height);
// create graphics memory buffer
m_graphics = Graphics.FromImage(m_bitmap);
m_graphics.GetHdc();
}
示例14: GetTextSize
public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags)
{
IntPtr hdc = IntPtr.Zero;
if ( graphics != null )
{
// Get device context from the graphics passed in
hdc = graphics.GetHdc();
}
else
{
// Get screen device context
hdc = WindowsAPI.GetDC(IntPtr.Zero);
}
IntPtr fontHandle = font.ToHfont();
IntPtr currentFontHandle = WindowsAPI.SelectObject(hdc, fontHandle);
Win32.RECT rect = new Win32.RECT();
rect.left = rc.Left;
rect.right = rc.Right;
rect.top = rc.Top;
rect.bottom = rc.Bottom;
WindowsAPI.DrawText(hdc, text, text.Length, ref rect, (int)drawFlags);
WindowsAPI.SelectObject(hdc, currentFontHandle);
WindowsAPI.DeleteObject(fontHandle);
if ( graphics != null )
graphics.ReleaseHdc(hdc);
else
WindowsAPI.ReleaseDC(IntPtr.Zero, hdc);
return new Size(rect.right - rect.left, rect.bottom - rect.top);
}
示例15: MeasureString
/// <summary>
/// Measure a multiline string
/// </summary>
public static Size MeasureString(Graphics gr, Font font, string text, int width)
{
if (text == null) return new Size(1, 1);
Rect bounds = new Rect() { Left = 0, Right = width, Bottom = 1, Top = 0 };
IntPtr hDc = gr.GetHdc();
try
{
int flags = DTCALCRECT | DTWORDBREAK;
IntPtr controlFont = font.ToHfont();
IntPtr originalObject = SelectObject(hDc, controlFont);
try
{
DrawText(hDc, text, text.Length, ref bounds, flags);
}
finally
{
SelectObject(hDc, originalObject); // Release resources
}
}
finally
{
gr.ReleaseHdc(hDc);
}
return new Size(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top);
}