Enumerating monitors on a computer

I don’t know all of these API’s but I do remember some of them (bad memories) so here’s what I can remember and find from poking around in MSDN and playing with wbemtest which I’m surprised I even remember. I recognize that this answer is probably not ALL that you were hoping for. For the … Read more

How to read the Color of a Screen Pixel

This is the most efficient: It grabs a pixel at the location of the cursor, and doesn’t rely on only having one monitor. using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Diagnostics; namespace FormTest { public partial class Form1 : Form { [DllImport(“user32.dll”)] static extern bool GetCursorPos(ref Point lpPoint); [DllImport(“gdi32.dll”, CharSet = … Read more

C# – Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App

The immediately usable code public class DirectBitmap : IDisposable { public Bitmap Bitmap { get; private set; } public Int32[] Bits { get; private set; } public bool Disposed { get; private set; } public int Height { get; private set; } public int Width { get; private set; } protected GCHandle BitsHandle { get; … Read more

Image.Save(..) throws a GDI+ exception because the memory stream is closed

As it’s a MemoryStream, you really don’t need to close the stream – nothing bad will happen if you don’t, although obviously it’s good practice to dispose anything that’s disposable anyway. (See this question for more on this.) However, you should be disposing the Bitmap – and that will close the stream for you. Basically … Read more

Resize image proportionally with MaxHeight and MaxWidth constraints

Like this? public static void Test() { using (var image = Image.FromFile(@”c:\logo.png”)) using (var newImage = ScaleImage(image, 300, 400)) { newImage.Save(@”c:\test.png”, ImageFormat.Png); } } public static Image ScaleImage(Image image, int maxWidth, int maxHeight) { var ratioX = (double)maxWidth / image.Width; var ratioY = (double)maxHeight / image.Height; var ratio = Math.Min(ratioX, ratioY); var newWidth = (int)(image.Width … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)