Retrieve only image tag from TinyMCE editor in ASP.Net using RegularExpression in C# and VB.Net

on Feb 26, 2018 05:38 AM

Dear Sir/Madam,

i am using the tinymce editor and i am saving the content in db.

its working fine.

but the problem is that how can i hide all the html tags(of the content data) leaving the image tag on page load.

is that possible to remove all html tags leaving one html tag.

thanks for your help in advance.



Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Feb 26, 2018 06:47 AM
on Feb 26, 2018 11:01 PM

You cannot hide all the tag forTinyMCE Editor. But you can get only image tag content onces you are saving the content.

RichTextBox Example using Free TinyMCE Editor in ASP.Net

Refer the below sample code for your reference.


<html xmlns="">
<head runat="server">
    <title>TinyMCE ASP.Net Example</title>
    <script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript">
        mode: "textareas",
        theme: "advanced",
        plugins: "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",       
        theme_advanced_buttons2: "image",
       theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
    <form id="form1" runat="server">
        <asp:TextBox ID="RichTextBox" runat="server" TextMode="MultiLine"></asp:TextBox>
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
        <asp:Label ID="lblDisplay" runat="server" Text=""></asp:Label>


protected void btnSave_Click(object sender, EventArgs e)
    lblDisplay.Visible = true;
    RichTextBox.Visible = false;
    List<string> imagestring = GetImagesInHTMLString(RichTextBox.Text);
    foreach (string imagetag in imagestring)
        lblDisplay.Text += imagetag + "<br/>";
private List<string> GetImagesInHTMLString(string htmlString)
    List<string> images = new List<string>();
    string pattern = @"<(img)\b[^>]*>";

    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(htmlString);

    for (int i = 0, i< matches.Count;i++)

    return images;


Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    lblDisplay.Visible = True
    RichTextBox.Visible = False
    Dim imagestring As List(Of String) = GetImagesInHTMLString(RichTextBox.Text)
    For Each imagetag As String In imagestring
        lblDisplay.Text += imagetag & "<br/>"
End Sub

Private Function GetImagesInHTMLString(ByVal htmlString As String) As List(Of String)
    Dim images As List(Of String) = New List(Of String)()
    Dim pattern As String = "<(img)\b[^>]*>"
    Dim rgx As Regex = New Regex(pattern, RegexOptions.IgnoreCase)
    Dim matches As MatchCollection = rgx.Matches(htmlString)
    Dim i As Integer = 0
    Do While (i < matches.Count)
      i = (i + 1)

    Return images
End Function