0

I want convert Color to hexadecimal (like #FFDA2366). I know, there is a lot of topic like mine but the sollution like

Color color = (Color)ColorConverter.ConvertFromString(paint);

or rect.Fill = (Brush)(new BrushConverter()).ConvertFromString(paint); don't work. Any idea?

I have class Rect in separate file like class choose. In main file I create static variable paint -for now.

class Rect
{
    public int Width { get; set; }
    public int Height { get; set; }
    public int Left { get; set; }
    public int Top { get; set; }
    public System.Windows.Media.Brush Color { get; set; }

}


 class choose
{
    public void rectangle(int x, int y, int w, int h,string paint,  Canvas canvas)
    {
      // var converter = new System.Windows.Media.BrushConverter();
       //var brush = (Brush)converter.ConvertFromString("#FFFFFF90");


        List<Rect> rects = new List<Rect>();
        rects.Add(new Rect()
        {
            Width = x,
            Height = y,
            Left = w,
            Top = h,
            Color = System.Windows.Media.Brushes.Purple // instead of Purple I want to be a variable paint
        });
        foreach (Rect rect in rects)
        {
            System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
            r.Width = rect.Width;
            r.Height = rect.Width;
            r.Fill = rect.Color;
            Canvas.SetLeft(r, rect.Left);
            Canvas.SetTop(r, rect.Top);


            canvas.Children.Add(r);

        }

    }
}
private void rectangle_Click(object sender, RoutedEventArgs e)
    {
        choose r1 = new choose();
        string paint = "#FFA669D1";

        int x = int.Parse(beginx.Text);
        int y = int.Parse(beginy.Text);
        int w = int.Parse(wid.Text);
        int h = int.Parse(hei.Text);

        if (!((x > canvas.ActualWidth) || (y > canvas.ActualHeight) || (w > canvas.ActualWidth) ||(h > canvas.ActualHeight)))
        {
            r1.rectangle(x, y, w, h, paint, canvas);
        }
        else
        {
            System.Windows.MessageBox.Show("err");
        }


    }
Meenti
  • 43
  • 1
  • 2
  • 6
  • #FFDA2366 is not a color. –  May 30 '17 at 11:50
  • @John yes it is. the first two decimals represent opacity. – Timothy Groote May 30 '17 at 11:52
  • @TimothyGroote TIL –  May 30 '17 at 11:52
  • @Meenti how exactly do those first two parts 'not work'? do they throw an exception? do they return a color you did not expect? – Timothy Groote May 30 '17 at 11:53
  • 2
    Controls do not support semi-transparent colors... see : https://stackoverflow.com/questions/24807182/removing-transparency-from-color –  May 30 '17 at 11:55
  • There is conflict between r.Fill because its represent System Windows Media and Color color = (Color)ColorConverter.ConvertFromString(paint); its represent System.Drawing. – Meenti May 30 '17 at 11:59
  • Then maybe you should specify which one you mean? (`System.Windows.Media.Color fillcolor = (System.Windows.Media.Color) ColorConverter.ConvertFromString(paint);` leaves nothing open to interpretation, so the compiler will understand you don't mean `System.Drawing.Color` – Timothy Groote May 30 '17 at 12:02
  • 1
    Btw, you already asked exactly this here : https://stackoverflow.com/questions/44244232/translate-color-to-hexadecimal-c-sharp and got answers that should solve your problem. voting to close this question – Timothy Groote May 30 '17 at 12:05
  • Why wouldn't this work?: rect.Fill = (Brush)(new BrushConverter()).ConvertFromString(paint); What happens? – mm8 May 30 '17 at 12:21
  • @Meenti You should carefully read my answer to your previous question. Provided that your `paint` variable is a string containing a valid color representation (e.g. `var paint = "#FFDA2366";`), it will work. – Clemens May 30 '17 at 15:26
  • @John The question is about WPF, where elements may well have semi-tramsparent background or fill brushes. Although two people seemingly found your comment useful, it is not applicable. – Clemens May 30 '17 at 18:48

0 Answers0