簡體   English   中英

如何將svg或xaml設置為圖像WPF的來源?

[英]How can i set svg or xaml as a source of image WPF?

我一直在網上搜索一種將svg設置為圖像源的方法,是最簡單的方法,到目前為止,我已經嘗試了許多不同的方法,並且有人為我工作。

我已將我的svg文件轉換為xaml文件,然后嘗試將其保存為ResourceDictionary,但沒有工作...我也嘗試使用此答案,但我無法找到我的svg文件支持的ContentControl ...

如果您能告訴我有關如何在我的wpf c#項目中設置svg的詳細步驟,我將非常感激。 這是我的png圖片以及我所做的所有其他嘗試。

在此處輸入圖片說明

我使用此鏈接將png轉換為svg。 然后我使用Inkscape將生成的svg轉換為xaml

這是我的代碼:

<Grid Background="#FFFFC000">
                <Viewbox Stretch="Uniform">
                    <Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Name="svg3336" Width="150" Height="96">
                        <Canvas.RenderTransform>
                            <TranslateTransform X="0" Y="0"/>
                        </Canvas.RenderTransform>
                        <Canvas.Resources/>
                        <Canvas Name="g3340">
                            <Canvas.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform ScaleX="0.100000" ScaleY="-0.100000"/>
                                    <TranslateTransform X="0.000000" Y="96.000000"/>
                                </TransformGroup>
                            </Canvas.RenderTransform>
                            <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path3342" Fill="White" Data="M10 479 l0 -472 168 6 c313 12 573 94 797 252 104 73 272 246 339 349 61 92 126 216 126 239 0 7 4 17 9 23 4 5 13 24 19 42 l11 32 -735 0 -734 0 0 -471z m242 445 l178 -6 -52 -47 c-60 -54 -68 -58 -68 -36 0 29 -27 45 -74 45 -60 0 -72 -18 -68 -99 5 -80 22 -69 22 15 l0 64 50 0 c48 0 50 -1 50 -28 0 -23 -20 -46 -110 -127 -60 -54 -112 -105 -115 -113 -6 -18 20 -61 42 -69 9 -3 24 -1 34 6 18 11 19 5 19 -141 0 -192 -3 -188 125 -188 103 0 105 1 105 68 0 75 7 83 64 80 l51 -3 3 -60 c2 -32 8 -65 14 -72 15 -18 191 -18 206 0 7 8 12 74 13 169 l1 157 24 -11 c21 -10 28 -8 50 12 14 14 24 33 22 45 -2 12 -77 83 -184 175 l-180 155 479 3 c264 1 482 1 484 -1 8 -9 -60 -157 -108 -237 -244 -402 -679 -640 -1179 -644 l-115 -1 -3 435 c-1 239 0 440 2 446 5 14 12 14 218 8z m244 -59 c24 -20 49 -41 56 -48 7 -7 70 -61 141 -120 70 -60 127 -111 127 -115 -1 -10 -26 -42 -34 -42 -4 0 -73 57 -154 128 -81 70 -155 133 -164 141 -16 12 -33 1 -155 -105 -76 -65 -150 -129 -165 -143 -27 -24 -28 -24 -48 -6 -11 10 -19 22 -18 26 2 4 43 42 93 85 127 108 204 175 240 206 16 15 32 28 34 28 2 0 23 -16 47 -35z m88 -198 l129 -112 1 -167 1 -168 -93 0 -93 0 2 73 2 72 -81 3 -82 3 0 -76 0 -75 -90 0 -90 0 0 168 0 167 108 92 c59 50 116 100 127 112 11 11 22 20 25 20 3 0 63 -50 134 -112z"/>
                        </Canvas>
                    </Canvas>
                </Viewbox>
            </Grid>

如您所見,我將整個xaml文件添加到了我的代碼中,而不僅僅是調用它...

您可以將此代碼轉換為DrawingBrush。 (Blend在“工具”菜單中為其提供了一個命令。)然后,您可以在任意位置引用它。

這是結果代碼:

<DrawingBrush x:Key="svg3336" Viewbox="0,0,150,96" ViewboxUnits="Absolute">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="White" Geometry="M10,479L10,7 178,13C491,25 751,107 975,265 1079,338 1247,511 1314,614 1375,706 1440,830 1440,853 1440,860 1444,870 1449,876 1453,881 1462,900 1468,918L1479,950 744,950 10,950 10,479z M252,924L430,918 378,871C318,817 310,813 310,835 310,864 283,880 236,880 176,880 164,862 168,781 173,701 190,712 190,796L190,860 240,860C288,860 290,859 290,832 290,809 270,786 180,705 120,651 68,600 65,592 59,574 85,531 107,523 116,520 131,522 141,529 159,540 160,534 160,388 160,196 157,200 285,200 388,200 390,201 390,268 390,343 397,351 454,348L505,345 508,285C510,253 516,220 522,213 537,195 713,195 728,213 735,221 740,287 741,382L742,539 766,528C787,518 794,520 816,540 830,554 840,573 838,585 836,597 761,668 654,760L474,915 953,918C1217,919 1435,919 1437,917 1445,908 1377,760 1329,680 1085,278 650,40 150,36L35,35 32,470C31,709 32,910 34,916 39,930 46,930 252,924z M496,865C520,845 545,824 552,817 559,810 622,756 693,697 763,637 820,586 820,582 819,572 794,540 786,540 782,540 713,597 632,668 551,738 477,801 468,809 452,821 435,810 313,704 237,639 163,575 148,561 121,537 120,537 100,555 89,565 81,577 82,581 84,585 125,623 175,666 302,774 379,841 415,872 431,887 447,900 449,900 451,900 472,884 496,865z M584,667L713,555 714,388 715,220 622,220 529,220 531,293 533,365 452,368 370,371 370,295 370,220 280,220 190,220 190,388 190,555 298,647C357,697 414,747 425,759 436,770 447,779 450,779 453,779 513,729 584,667z"/>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM