title: c# get top left pixel of "system.windows.controls.image"
body: <p><strong>In short:</strong></p>
  
  <p>My goal is to make a button control in WPF/C#/XAML that can have an system.windows.controls.image (from now on: image) on it as content, and I want to take it's top-left pixel and make the rest of the content background same color as the image has. The pictures for the images are resources for the project. Now my problem is, that I simply can not get the top-left pixel OR I can not get the picture on the button. I tried several ways after some googling.</p>
  
  <p>First:
  I use pictures as embedded resources.
  This way I can get the top-left pixel by loading the first 4 byte of the picture, but the picture will not show up on the button.</p>
  
  <p>Second:
  I use pictures as resources.
  This way I can see the images on the button, but I can not get the pixel color, because when I try to access the picture, it will break the program with error message about the picture not existing.</p>
  
  <p><strong>In detail:</strong></p>
  
  <p>The button in app.xaml:</p>
  
  <pre><code>&lt;Style TargetType="Button" BasedOn="{StaticResource GombAlap}" x:Key="GombM"&gt;
    &lt;!-- BasedOn="{StaticResource {x:Type Button}}" --&gt;
    &lt;Setter Property="Width" Value="200" /&gt;
    &lt;Setter Property="Margin" Value="0,0,10,0" /&gt;
    &lt;Setter Property="Template"&gt;
      &lt;Setter.Value&gt;
        &lt;ControlTemplate TargetType="Button"&gt;
          &lt;Grid&gt;
            &lt;Grid Background="{TemplateBinding Background}"  Opacity="0.2" /&gt;
            &lt;DockPanel Margin="{TemplateBinding Padding}"&gt;
              &lt;Grid Background="White" DockPanel.Dock="Bottom" Visibility="{TemplateBinding Tag, Converter={StaticResource StringEmptyVisibleVagyCollapse}}"&gt;
                &lt;TextBlock Text="{TemplateBinding Tag}" /&gt;
              &lt;/Grid&gt;
              &lt;!-- kép --&gt;
              &lt;Viewbox Margin="{TemplateBinding Padding}"&gt;
                &lt;ContentPresenter Margin="{TemplateBinding Padding}" /&gt;
              &lt;/Viewbox&gt;
            &lt;/DockPanel&gt;
          &lt;/Grid&gt;
        &lt;/ControlTemplate&gt;
      &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>This is a simple stuff: I use button.tag for the text (if none given, I hide the area). The content can be anything, and covers the rest of the button.</p>
  
  <p>Code I try to use (sorry for not translating the comments):</p>
  
  <pre><code>ÚjMenüpont("Exit to Windows", FStatic.ImageResourceből("../Gombok/Windows8.png"), delegate { Close(); }); //kilépés gomb
  </code></pre>
  
  <p>This calls the button initializer. Params are: text, image, and click handler. So this simply will close the main window. Let's see what loads the image first:</p>
  
  <pre><code>public static Image ImageResourceből(string Név)
  {
    Image eredmény = new Image();
    BitmapImage bi = new BitmapImage(new Uri(Név, UriKind.RelativeOrAbsolute));
    eredmény.Source = bi;
    return eredmény;
  }
  </code></pre>
  
  <p>This actually works.. loads the image, and that's it. Let's see the ÚjMenüpont code I called above:</p>
  
  <pre><code>void ÚjMenüpont(string Név, object Tartalom, Action Feladat)
  {
    Button új = new Button(); //új gomb
    új.Tag = Név; //ez lesz az alsó szöveg
    új.Content = Tartalom; //ez az objektum lesz a főrészen
  
    if (Tartalom is Image)
    {
      //ha tartalom kép, a színt a bal felső sarokhoz igazítom
      Image képként = Tartalom as Image; //képként értelmezem
      var balfelső = new byte[4]; //buffer
  
      Assembly futtató = Assembly.GetExecutingAssembly();
      System.Drawing.Image kép = System.Drawing.Bitmap.FromStream(futtató.GetManifestResourceStream(
        futtató.FullName.Substring(0, futtató.FullName.IndexOf(",")).Replace(" ", "_").Replace("../", string.Empty) + "." + képként.Source.ToString().Replace("../", string.Empty).Replace("/", ".")
      ));
      System.Drawing.Color szín = ((System.Drawing.Bitmap)(kép)).GetPixel(0, 0);
      új.Background = new SolidColorBrush(Color.FromRgb(szín.R, szín.G, szín.B));
  
      //új.Content = ZXF.Konvertálók.KonvertálImagera.Graphicsból(System.Drawing.Graphics.FromImage(kép));
    }
  
    új.Click += new RoutedEventHandler( //gombnyomáskor ez történik
      delegate(object sender, RoutedEventArgs args) //anoním eljárás click paramétereivel
      {
        //végrehajtom a feladatot
        Feladat(); //ami a paraméter
      }
    );
    Menü.Children.Add(új); //menüsávra teszem
  }
  </code></pre>
  
  <p>First it creates the button, then passes the text into the tag. Checks if content is an image. If yes, it will try to get the top-left pixel. Now...</p>
  
  <p>When the picture is embedded resource, the program will run but will not show the image (only from designer, somewhy), but the button background will be just perfect.</p>
  
  <p><img src="https://i.stack.imgur.com/OVo51.png" alt="enter image description here"></p>
  
  <p>From designer it looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/Kc6ba.png" alt="enter image description here"></p>
  
  <p>But when I try to use as resource (instead of embedded resource), I will get a break, because there is no such resource available:</p>
  
  <p><img src="https://i.stack.imgur.com/Frlhw.png" alt="enter image description here"></p>
  
  <p>I checked other methods to get the top-left corner, but when I ran the code I got message that  does not exists - in runtime, for resource... so far this one is the best working code I could put together.</p>
  
  <p><strong>So the question is:</strong></p>
  
  <p>How could I show the picture too, or how can I get the top-left pixel?</p>