2012-11-20 190 views
0

使用DrawingImage爲矩形我在XAML創建了一個DrawingImage並設置它的矩形在代碼隱藏

XAML:

<DrawingBrush.Drawing> 
       <DrawingGroup> 
        <GeometryDrawing Geometry="M 126.889,143.333L 250.889,143.333L 250.889,267.333L 126.889,267.333L 126.889,143.333 Z "> 
         <GeometryDrawing.Brush> 
          <LinearGradientBrush StartPoint="0,0.0215053" EndPoint="1,1"> 
           <GradientStop Color="#FFFFFFFF" Offset="0.00847458" /> 
           <GradientStop Color="#FF949063" Offset="0.800847" /> 
          </LinearGradientBrush> 
         </GeometryDrawing.Brush> 
        </GeometryDrawing> 
        <GeometryDrawing Brush="#FF85825F" Geometry="F1 M 162.428,200.167C 162.428,184.671 175.003,172.085 190.509,172.085C 206.005,172.085 218.591,184.671 218.591,200.167C 218.591,215.683 206.005,228.249 190.509,228.249C 175.003,228.249 162.428,215.683 162.428,200.167 Z " /> 
        <GeometryDrawing Brush="#FF524F40" Geometry="F1 M 160.598,199.025C 160.598,183.529 173.174,170.943 188.68,170.943C 204.176,170.943 216.762,183.529 216.762,199.025C 216.762,214.541 204.176,227.107 188.68,227.107C 173.174,227.107 160.598,214.541 160.598,199.025 Z " /> 
        <GeometryDrawing Brush="#FF6D6B51" Geometry="F1 M 253.277,270.359C 250.065,261.438 253.089,268.956 244.895,249.789C 242.347,243.406 234.03,235.753 223.176,235.753L 163.176,235.753C 152.342,235.753 144.025,243.406 141.488,249.789C 138.94,256.153 135.803,261.438 132.631,270.359" /> 
        <GeometryDrawing Brush="#FF524F40" Geometry="F1 M 249.41,266.847C 246.198,257.926 249.222,265.444 241.028,246.278C 238.48,239.894 230.163,232.241 219.309,232.241L 159.309,232.241C 148.475,232.241 140.158,239.894 137.621,246.278C 135.074,252.641 131.936,257.926 128.764,266.847" /> 
        <GeometryDrawing Brush="#FFFFFFFF" Geometry="M 115.999,132.639L 260.777,132.639L 260.777,301.028L 115.999,301.028L 115.999,132.639 Z M 126.208,142.884L 250.208,142.884L 250.208,266.884L 126.208,266.884L 126.208,142.884 Z "> 
         <GeometryDrawing.Pen> 
          <Pen LineJoin="Round" Brush="#FFC0C1C3" /> 
         </GeometryDrawing.Pen> 
        </GeometryDrawing> 
       </DrawingGroup> 
      </DrawingBrush.Drawing> 

我想將它設置爲後面的代碼。

我使用此代碼,但不顯示DrawingImage。

代碼隱藏

GradientStopCollection collection = new GradientStopCollection(); 
      collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFFFFFF"), 0.00847458)); 
      collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF949063"), 0.800847)); 
      LinearGradientBrush gbrush = new LinearGradientBrush(collection, new System.Windows.Point(0, 0.0215053), new System.Windows.Point(1, 1)); 

      var dGroup = new DrawingGroup(); 
      using (DrawingContext dc = dGroup.Open()) 
      { 
       dc.DrawGeometry(gbrush, null, Geometry.Parse("M 126.889,143.333L 250.889,143.333L 250.889,267.333L 126.889,267.333L 126.889,143.333 Z ")); 
       dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF85825F")), null, Geometry.Parse("F1 M 162.428,200.167C 162.428,184.671 175.003,172.085 190.509,172.085C 206.005,172.085 218.591,184.671 218.591,200.167C 218.591,215.683 206.005,228.249 190.509,228.249C 175.003,228.249 162.428,215.683 162.428,200.167 Z ")); 
       dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 160.598,199.025C 160.598,183.529 173.174,170.943 188.68,170.943C 204.176,170.943 216.762,183.529 216.762,199.025C 216.762,214.541 204.176,227.107 188.68,227.107C 173.174,227.107 160.598,214.541 160.598,199.025 Z ")); 
       dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF6D6B51")), null, Geometry.Parse("F1 M 253.277,270.359C 250.065,261.438 253.089,268.956 244.895,249.789C 242.347,243.406 234.03,235.753 223.176,235.753L 163.176,235.753C 152.342,235.753 144.025,243.406 141.488,249.789C 138.94,256.153 135.803,261.438 132.631,270.359")); 
       dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 249.41,266.847C 246.198,257.926 249.222,265.444 241.028,246.278C 238.48,239.894 230.163,232.241 219.309,232.241L 159.309,232.241C 148.475,232.241 140.158,239.894 137.621,246.278C 135.074,252.641 131.936,257.926 128.764,266.847")); 
       dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFFFF")), new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC0C1C3")), 0) { LineJoin = PenLineJoin.Round }, Geometry.Parse("M 115.999,132.639L 260.777,132.639L 260.777,301.028L 115.999,301.028L 115.999,132.639 Z M 126.208,142.884L 250.208,142.884L 250.208,266.884L 126.208,266.884L 126.208,142.884 Z ")); 
      } 

      return new DrawingImage(dGroup); 

回答

0

你可以做類似

private DrawingBrush GetDBrush() 
    { 
     GradientStopCollection collection = new GradientStopCollection(); 
     collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFFFFFF"), 0.00847458)); 
     collection.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF949063"), 0.800847)); 
     LinearGradientBrush gbrush = new LinearGradientBrush(collection, new System.Windows.Point(0, 0.0215053), new System.Windows.Point(1, 1)); 

     var dGroup = new DrawingGroup(); 
     using (DrawingContext dc = dGroup.Open()) 
     { 
      dc.DrawGeometry(gbrush, null, Geometry.Parse("M 126.889,143.333L 250.889,143.333L 250.889,267.333L 126.889,267.333L 126.889,143.333 Z ")); 
      dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF85825F")), null, Geometry.Parse("F1 M 162.428,200.167C 162.428,184.671 175.003,172.085 190.509,172.085C 206.005,172.085 218.591,184.671 218.591,200.167C 218.591,215.683 206.005,228.249 190.509,228.249C 175.003,228.249 162.428,215.683 162.428,200.167 Z ")); 
      dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 160.598,199.025C 160.598,183.529 173.174,170.943 188.68,170.943C 204.176,170.943 216.762,183.529 216.762,199.025C 216.762,214.541 204.176,227.107 188.68,227.107C 173.174,227.107 160.598,214.541 160.598,199.025 Z ")); 
      dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF6D6B51")), null, Geometry.Parse("F1 M 253.277,270.359C 250.065,261.438 253.089,268.956 244.895,249.789C 242.347,243.406 234.03,235.753 223.176,235.753L 163.176,235.753C 152.342,235.753 144.025,243.406 141.488,249.789C 138.94,256.153 135.803,261.438 132.631,270.359")); 
      dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF524F40")), null, Geometry.Parse("F1 M 249.41,266.847C 246.198,257.926 249.222,265.444 241.028,246.278C 238.48,239.894 230.163,232.241 219.309,232.241L 159.309,232.241C 148.475,232.241 140.158,239.894 137.621,246.278C 135.074,252.641 131.936,257.926 128.764,266.847")); 
      dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFFFF")), new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFC0C1C3")), 0) { LineJoin = PenLineJoin.Round }, Geometry.Parse("M 115.999,132.639L 260.777,132.639L 260.777,301.028L 115.999,301.028L 115.999,132.639 Z M 126.208,142.884L 250.208,142.884L 250.208,266.884L 126.208,266.884L 126.208,142.884 Z ")); 
     } 

     return new DrawingBrush(dGroup); 
    } 

要顯示它在你的矩形,使用填充屬性:

Rectangle.Fill = GetDBrush(); 
+0

非常感謝......我在XAML的轉換器和綁定FillProperty中使用此代碼 – Niloo

+0

非常感謝:) – Niloo

+0

不客氣。 ;) –