gdi+

Fastest way to draw an overlay on an image in windows forms

What is the fastest way to draw an on an image in .net? I'm trying to draw an image on top of another image that have in a windows form control. I know that when I try to draw it directly using loops it takes an Eon. Options I have heard tossed around are GDI+, Direct Draw, or DX10. I start with a bitmap of a fixed size and after apply...

Antialiased text on transparent bitmap

I'd like to draw antialiased text on a transparent bitmap and have the antialiasing drawn as alpha blended pixels. This way, I can draw the bitmap onto any color surface (or an image, for that matter) and the antialiasing still looks fine. Here is a simplified sample showing the problem: protected override void OnPaint(PaintEventArgs e...

Rules of Thumb in GDI+

I have been working on some GDI+ code in .NET and have been learning my lessons the hard way. Simple things like: What looks good on screen may not look nice on paper and vice versa Caching too many objects can result in an OutOfMemoryException Floats aren't exact ...and so on. I'm sure there is a lot more that experienced folk can a...

Transparent window containing opaque text and buttons

I'm creating a non-intrusive popup window to notify the user when processing a time-consuming operation. At the moment I'm setting its transparency by calling SetLayeredWindowAttributes which gives me a reasonable result: However I'd like the text and close button to appear opaque (it doesn't quite look right with white text) while k...

C# 2D collision detection problem

Hello! I am stuck trying to figure out how to alter my collision detection to work correctly, i got all my wall objects stacked inside a List and then when the player moves i loop thru each wall object and call the DetectCollision method, this returns true or false depending on if the object is inside the wall or not. Wall detect colli...

How does GraphicsPath.AddArc use the startAngle and sweepAngle parameters?

I am trying to use System.Drawing.Drawing2D.GraphicsPath.AddArc to draw an arc of an ellipse starting at 0 degrees and sweeping to 135 degrees. The issue I am running in to is that for an ellipse, the arc drawn does not match up with what I would expect. For example, the following code generates the image below. The green circles are w...

Fastest way to generate bitmap from polygons in .NET

After reading this blog, I want to try writing a similar program myself. One of the subtasks is to have a source set of polygons, and use these polygons to generate the bitmap that is going to be compared with the target image. In .NET 2005 the obvious method is to use GDI+. Simply create a bitmap, get a Graphics object that interfaces...

Non-scaling Pen in GDI+

I need to create a pen which draws with a width of 0.5 mm regardless of the scale of the graphics transformation matrix. Here is what I am doing: Note: The sample code is C++/CLI but answers using C# are welcome. // assume that g is a Graphics object int dpi = g->DpiX; float dotsPerHalfInch = dpi * 0.5; // to extract scale factor from...

Graphics.Save vs Graphics.BeginContainer

How is Graphics.Save different from Graphics.BeginContainer? ...

c# GDI+, Creating a LinearGradientBrush in a loop (memory leaks)

Dear colleagues. Today I came across a bit of a dilema. I have created an app that uses GDI+ to draw on a form. The drawing is triggered by a timer every second. The draw method uses a for loop to iterate through a collection of objects and if they are of a certain state, draw them. I want to draw them using a LinearGradientBrush simpl...

How might I convert a Rectangle to a RectangleF?

What is the easiest way to convert a Rectangle to a RectangleF in .NET? Edit: This sounds trivial and it is, but I was trying to save some typing. The best I could come up with: RectangleF rdest(rsrc.Location, rsrc.Size); // C++/CLI ...or... RectangleF rdest = new RectangleF(rsrc.Location, rsrc.Size) // C# ...

GDI+: How do I draw a line that's one inch in length on any device it's drawn on?

I need to draw a line one inch long on any device given a Graphics reference to it. I need it to be an inch long regardless of what Transform is set to. Let's assume that the scaling factor of the transform is given by scale in both horizontal and vertical directions. Some C++/CLI code: g->DrawLine(Pens::Black, 50.0f, 50.0f, 50.0f + on...

How can I get the exact location of cursor on image in Picturebox

Hi every one. I have the picture box in which the image may be zoomed with different values. Then I try to find out the location of cursor on picture box. I write the following code on picture box mouse move event: int x = (2 * e.X - pictureBox1.Width + pictureBox1.Image.Width) / (2 * _scale / 100); int y = (2 * e.Y - pictureBox1.Height...

Image resizing efficiency in C# and .NET 3.5

I have written a web service to resize user uploaded images and all works correctly from a functional point of view, but it causes CPU usage to spike every time it is used. It is running on Windows Server 2008 64 bit. I have tried compiling to 32 and 64 bit and get about the same results. The heart of the service is this function: p...

Creating a Hand-Drawn effect using .NET

I'd like to write a piece of code which creates images similar in style to those rendered by 'Balsamiq Mockups' or 'yUML' using the .NET framework. Can anybody tell me how to achieve the hand-drawn pencil effect using GDI+? The text can obviously be done using the right font - my question is how to render the lines, boxes and circles. ...

How do I Draw a Bitmap with 50% Opacity?

Hello, I have a .png file, with alpha channel, that I use as the BackgroundImage on a Panel control. Under certain circumstances the control is disabled. When it is disabled I want the background image to be 50% transparent so that the user gets some sort of visual indication as to the controls state. Does anyone know how I can make ...

Compress image in .NET

How can i compress and image file(bmp,jpeg) in C#, I have to display some images as background on my controls, i m using following code to scale my image Bitmap orgBitmap = new Bitmap(_filePath); Bitmap regBitmap = new Bitmap(reqSize.Width, reqSize.Height); using (Graphics gr = Graphics.FromImage(reqBitmap)) { gr.SmoothingMode = S...

WinSxS: How to take dependency on specific version of gdiplus.dll?

I need to add a dependency on a specific version of GDIPlus. This is the version of GDI+ that i want: I want to be sure that I'm using this version for a specific compatibility reason. I've added an assembly manifest to my executable, defining my dependancy on the version of GdiPlus: <dependency> <dependentAssembly> <assemb...

Transparency around non-rectangular (owner draw) ToolTip control?

I'm customizing the appearance of a WinForms ToolTip control by responding to the Draw event. I just want some of the ToolTip's corners to be rounded. I've got everything working such that the first time the ToolTip is displayed, everything looks perfect. On subsequent displays, however, the unfilled areas of my rounded rectangle continu...

Problem on Windows 7 using Image.FromStream to open cmyk+alpha tiff file

I'm having a problem running the following code om Windows 7 x86 when creating an Image from a lzw encoded cmyk + alpha TIFF file. The FromStream call throws a System.ArgumentException: Parameter is not validRunning When I run the code on Vista or Server 2008 (both x86 and x64 bit) it just works. using System; using System.Drawing; us...