MatteoP
MatteoP
Reputation Top 1%
Matteo Piovanelli
10 Snippets  (92nd place)
Published
0 Channels
Created
3 Channels
Following
226 points  (135th place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Senior Famous Coder
Senior Popular Coder

Recent Snippets See all snippets by MatteoP

public by MatteoP  2104  5  6  1

Visual Studio: count LoC

from http://www.codeproject.com/Articles/625710/Line-Counting-Trickery-within-Visual-Studio
Press CTRL+SHIFT+F to present the Find in Files prompt.
Select Use Regular Expression under the Find Options section.
Select the scope of your Search using the Look In drop-down.
Further determine the type of files that you wish to search within the Look at these File Types box.
Enter the following Regular Expressions within the Find What box:

^(?([^\r\n])\s)*[^\s+?/]+[^\n]*$

;

public by MatteoP  1571  2  6  0

textbox for integers

//textbox keypress handler to only allow integers
private void tbInteger_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
;

public by MatteoP  2829  1  6  0

textbox for doubles

//handler for the keyPress event on a textbox to only allow writing doubles
private void tbDouble_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar)
                && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }
;

public by MatteoP  3172  6  6  0

Convert click coordinates form picture box to image

//Convert coordinates of click from PictureBox reference to Image reference.

private void pbBOX_MouseClick(object sender, MouseEventArgs e)
        {
            //we need to convert the coordinates of the click event from those of the picture box to those of the image
            int X = e.Location.X;
            int Y = e.Location.Y;
            if (pbBOX.Image != null)
            {
                switch (pbBOX.SizeMode)
                {
                    case PictureBoxSizeMode.AutoSize:
                        //The PictureBox is sized equal to the size of the image that it contains.
                        //The coordinates related to the picturebox are the same as those related to the image.
                        break;
                    case PictureBoxSizeMode.CenterImage:
                        //The image is displayed in the center if the PictureBox is larger than the image.
                        //If the image is larger than the PictureBox, the picture is placed in the center of the PictureBox and the outside edges are clipped.
                        int diffWidth = (pbBOX.Width - pbBOX.Image.Width) / 2;
                        int diffHeight = (pbBOX.Height - pbBOX.Image.Height) / 2;
                        X -= diffWidth;
                        Y -= diffHeight;
                        break;
                    case PictureBoxSizeMode.Normal:
                        //The image is placed in the upper-left corner of the PictureBox. The image is clipped if it is larger than the PictureBox it is contained in.
                        //The coordinates related to the picturebox are the same as those related to the image.
                        break;
                    case PictureBoxSizeMode.StretchImage:
                        //The image within the PictureBox is stretched or shrunk to fit the size of the PictureBox.
                        if (!(pbBOX.Width == 0 || pbBOX.Height == 0))
                        {
                            double ratioWidth = (double)pbBOX.Image.Width / (double)pbBOX.Width;
                            double ratioHeight = (double)pbBOX.Image.Height / (double)pbBOX.Height;
                            X = (int)(X * ratioWidth);
                            Y = (int)(Y * ratioHeight);
                        }
                        break;
                    case PictureBoxSizeMode.Zoom:
                        //The size of the image is increased or decreased maintaining the size ratio.
                        if (!(pbBOX.Width == 0 || pbBOX.Height == 0 || pbBOX.Image.Width == 0 || pbBOX.Image.Height == 0))
                        {
                            double imAspectRatio = (double)pbBOX.Image.Width / (double)pbBOX.Image.Height;
                            double pbAspectRatio = (double)pbBOX.Width / (double)pbBOX.Height;
                            if (imAspectRatio > pbAspectRatio)
                            {
                                //the limit is the width of the control
                                //the image fills the picture box from left to right
                                double ratioWidth = (double)pbBOX.Image.Width / (double)pbBOX.Width;
                                X = (int)(X * ratioWidth);
                                double scale = (double)pbBOX.Width / (double)pbBOX.Image.Width;
                                double diffH = ((double)pbBOX.Height - scale * (double)pbBOX.Image.Height) / 2;
                                Y = (int)(((double)Y - diffH) / scale);
                            }
                            else
                            {
                                //the limit is the height of the control
                                //the image fills the picture box from top to bottom
                                double ratioHeight = (double)pbBOX.Image.Height / (double)pbBOX.Height;
                                Y = (int)(Y * ratioHeight);
                                double scale = (double)pbBOX.Height / (double)pbBOX.Image.Height;
                                double diffW = ((double)pbBOX.Width - scale * (double)pbBOX.Image.Width) / 2;
                                X = (int)(((double)X - diffW) / scale);
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
      }
;

public by MatteoP  3593  3  6  1

Convert from HSV to ARGB .Net Color

//Converst from HSV values to a .net Color

public static Color HSVtoRGB(float hue, float saturation, float value, float alpha)
        {
            while (hue > 1f) { hue -= 1f; }
            while (hue < 0f) { hue += 1f; }
            while (saturation > 1f) { saturation -= 1f; }
            while (saturation < 0f) { saturation += 1f; }
            while (value > 1f) { value -= 1f; }
            while (value < 0f) { value += 1f; }
            if (hue > 0.999f) { hue = 0.999f; }
            if (hue < 0.001f) { hue = 0.001f; }
            if (saturation > 0.999f) { saturation = 0.999f; }
            if (saturation < 0.001f) { return Color.FromArgb((int)(value * 255f), (int)(value * 255f), (int)(value * 255f)); }
            if (value > 0.999f) { value = 0.999f; }
            if (value < 0.001f) { value = 0.001f; }

            float h6 = hue * 6f;
            if (h6 == 6f) { h6 = 0f; }
            int ihue = (int)(h6);
            float p = value * (1f - saturation);
            float q = value * (1f - (saturation * (h6 - (float)ihue)));
            float t = value * (1f - (saturation * (1f - (h6 - (float)ihue))));
            switch (ihue)
            {
                case 0:
                    return Color.FromArgb((int)(alpha * 255), (int)(value * 255), (int)(t * 255), (int)(p * 255));
                case 1:
                    return Color.FromArgb((int)(alpha * 255), (int)(q * 255), (int)(value * 255), (int)(p * 255));
                case 2:
                    return Color.FromArgb((int)(alpha * 255), (int)(p * 255), (int)(value * 255), (int)(t * 255));
                case 3:
                    return Color.FromArgb((int)(alpha * 255), (int)(p * 255), (int)(q * 255), (int)(value * 255));
                case 4:
                    return Color.FromArgb((int)(alpha * 255), (int)(t * 255), (int)(p * 255), (int)(value * 255));
                default:
                    return Color.FromArgb((int)(alpha * 255), (int)(value * 255), (int)(p * 255), (int)(q * 255));
            }
        }
;