简体   繁体   English

如何显示加载在 html 页面(Blazor ServerSide)上的变量中的 .svg 文件?

[英]How to show a .svg file which is loaded in a variable on a html page (Blazor ServerSide)?

I load a .svg file into a string edit that string.我将一个 .svg 文件加载到一个字符串中,编辑该字符串。 And now I want to display that edited string/.svg on the html page from my blazor project.现在我想在我的 blazor 项目的 html 页面上显示编辑过的字符串/.svg。

In windows forms I used在我使用的 Windows 窗体中

         //now show that svg icon in a picturebox (nuget 
         //https://github.com/vvvv/SVG)
         //input= the edit .svg String
         var mySvg = SvgDocument.FromSvg<SvgDocument>(input);
         PicbxShow.Image = mySvg.Draw(120,120);

If thought I use it in blazor like this:如果认为我像这样在 blazor 中使用它:

<img src=@mySvg.Draw(240,240) />

or directly like this或者直接这样

<svg xmlns="http://www.w3.org/2000/svg">@input</svg>

But the graphic is not shown.但是图形没有显示。 If I load it from file如果我从文件加载它

<img src="/images/TEST.svg" />

it works but I don't want to load->edit->save->load(edited)->display.它有效,但我不想加载->编辑->保存->加载(已编辑)->显示。

How do I display a .svg that is in my variable mySvg如何显示我的变量 mySvg 中的 .svg

    StreamReader reader = new StreamReader(@"TEST.svg");
    input = reader.ReadToEnd();
    mySvg = SvgDocument.FromSvg<SvgDocument>(input);

I don't have to use the nuget (svg) if there is an other solution.如果有其他解决方案,我不必使用 nuget (svg)。 Thank you for your Help and Time感谢您的帮助和时间


new tries新尝试

in the end I want to edit text "PHALT" in an .svg file and change that with other text (in blazor).最后,我想在 .svg 文件中编辑文本“PHALT”,并用其他文本(在 blazor 中)进行更改。 And then display the icon with the updated text.然后显示带有更新文本的图标。 I tried this now but still cant get it to work:我现在试过了,但仍然无法让它工作:

@page "/"
@using System.IO;

@* Load file  -> ICON is shown *@
<img src="/images/_temp_circle.svg" />
<br />

@* Open file _temp_circle.svg and copy the .svh text to blazor page manualy-> 
 ICON is shown *@

<svg xmlns="http://www.w3.org/2000/svg" height="7cm" width="7cm" 
 viewBox="00264.567 264.567"><g transform="matrix(3.77953 0 0 3.77953 
 -370.783 -731.84)"><circle cx="133.103" cy="228.633" r="24.892" 
  fill="#fff6d5" stroke="#a80" 
  stroke-width="2.074" stroke-linecap="round" />
 <path d="M133.103 208.228a3.058 3.058 0 00-3.065 3.065v23.567a7.42 7.42 0 
  00-4.355 6.757 7.42 7.42 0 007.42 7.42 7.42 7.42 0 007.42-7.42 7.42 7.420 
  00-4.355-6.757v-23.567a3.058 3.058 0 00-3.065-3.065z" opacity=".7" 
  fill="teal" stroke="#917c6f" stroke-width="1.219" stroke-linecap="square" 
  stroke-linejoin="round" />
  <text style="line-height:1.25;-inkscape-font-specification:'sans-serif 
  Bold'" x="111.943" y="233.039" font-weight="700" font-size="19.121" font- 
  family="sans-serif" letter-spacing="0" word-spacing="0" fill="#c8beb7" 
  stroke="#000" stroke-width=".695">
  <tspan x="111.943" y="233.039" style="-inkscape-font- 
   specification:'sans-serif Bold'">PHALT</tspan>
  </text></g></svg>
  <br />

  @* Read the svg file via stringreader and output the string here ->ICON 
     is NOT shown BUT the svg text is displayed as text WHY??? *@

 @input
 <br />

  <button class="btn btn-primary" @onclick="LoadSvg">Click me</button>

@code{ @代码{

  string path = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\images\"}";
  string input;


  private void LoadSvg()
  {
    StreamReader reader = new StreamReader(path + @"_temp_circle.svg");
    input = reader.ReadToEnd();

    input = input.Replace("PHALT", "TEST");
    reader.Close();
 }

}

You could render an image in HTML from base64 string like this:您可以从base64字符串以HTML呈现图像,如下所示:

<img src="data:image/svg+xml;base64,gdmVy...your-svg-bytes-as-base64-data-here.."/>

or similar but with another content type if you do that for a rasterized bitmap image.或类似,但如果您对光栅化位图图像执行此操作,则使用另一种内容类型。

I don't think the way you're trying to handle .svg will be the easiest in Blazor.我不认为您尝试处理 .svg 的方式在 Blazor 中是最简单的。 You can use <svg> tags (ie entire contents of any svg file) anywhere you would put <div> or other HTML tags in a Blazor component.您可以在将 <div> 或其他 HTML 标记放入 Blazor 组件的任何位置使用 <svg> 标记(即任何 svg 文件的全部内容)。

Then you can use @variable wherever you want to change or add any components, and add @code{} to change whatever you want, just like any other Blazor component.然后,您可以在任何想要更改或添加任何组件的地方使用 @variable,并添加 @code{} 来更改您想要的任何内容,就像任何其他 Blazor 组件一样。 So:所以:

(MySVGComponent.Blazor) (MySVGComponent.Blazor)

<svg blah blah blah>
    <polyline fill="none" stroke="#0074d9"
        stroke-width="2" points="@PointString" />
</svg>
@code {
     public string @PointString {get;set;}
     public void AddPoint (int X, int Y){
        @PointString += " " + X + "," + Y;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM