10 Eki

C# Get Width And Height of a Window Using Windows API

This example shows how to get width and height of a window using Windows API.

Usage :

            RECT rect;
            UserControl uc = new UserControl();
            if (GetWindowRect(new HandleRef(uc, uc.Handle), out rect))
            {                
                //To do anything  
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;
            }
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }
02 Eyl

Convert Image To Byte Array in C#

This example shows how to convert an image into a byte array.

Sample Code:

        public byte[] ImageToByteArray(Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

See Also:
Convert Byte Array To Image

02 Eyl

Convert Byte Array To Image in C#

This example shows how to convert a byte array into an image.

Sample Code:

        public Image ByteArrayToImage(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

See Also:
Convert Image To Byte Array

12 Tem

Select A Font With FontDialog Using C#

This example displays a font selection dialog box to select a font.

Usage:

            //Usage:
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                Font selectedFont = fontDialog.Font;
            }
12 Tem

Pick A Color With ColorDialog Using C#

This example displays a color selection window to select a color.

Usage:

            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                Color selectedColor = colorDialog.Color;
            }