-2

I tried a lot of methods, but couldn't get it exactly.

Note:winform richtextbox

Thanks for any reply.

image in Richtextbox

The following code is what I tried, but it doesn't get the exact size, maybe you know why.

Anyway, this code is not important, but how do you get the right size?

string rtfTxt = richTextBox1.SelectedRtf;
Match mat = Regex.Match(rtfTxt, @"picw[\d]+");
mat = Regex.Match(rtfTxt, @"picwgoal[\d]+");
//width of image
int width = int.Parse(mat.Value.Replace("picwgoal", "")) / 15;
mat = Regex.Match(rtfTxt, @"pichgoal[\d]+");

//height of image
int height = int.Parse(mat.Value.Replace("pichgoal", "")) / 15;

label2.Text = width + "x" + height;

View my description from the picture.

If convenient, you can also try this code, source links. https://pan.baidu.com/s/1dHfcBV7

dferenc
  • 7,918
  • 12
  • 41
  • 49
kfcalf
  • 15
  • 4
  • 2
    Look here: https://stackoverflow.com/questions/6376897/extracting-images-from-richtextbox there are plenty of solutions.. What have you tried? – Sasha Feb 14 '18 at 16:16
  • 2
    "I tried a lot of methods" - then consider sharing examples of those methods and describe the issues you encountered implementing them. I am certain that this question will be closed in its current form. – TnTinMn Feb 14 '18 at 16:17
  • 1
    You will probably need to analyze the rtf. There are _{\pict\wmetafile8\picw1234\pich1234_ numbers for width and height. You will need to check the rtf format description, which looks hard but insn't.. – TaW Feb 14 '18 at 16:29
  • Thank you for your kindness. It may not be as simple as that when you try. – kfcalf Feb 15 '18 at 01:32

1 Answers1

0

Based on @Taw's comment, you can use the pictsize block to extract the size:

rtb.SelectionChanged += (s, e) =>
{
    var pictureSizes = Regex.Matches(rtb.SelectedRtf.Dump(), @"\\picw(?<picw>\d+)\\pich(?<pich>\d+)(\\picwgoal(?<picwgoal>\d+))?(\\pichgoal(?<pichgoal>\d+))?")
        .Cast<Match>()
        .Select(m => m.Groups)
        .Select(grp => new
        {
            Width = int.Parse(grp["picw"].Value),
            Height = int.Parse(grp["pich"].Value),
            DesiredWidth = grp["picwgoal"].Success ? int.Parse(grp["picwgoal"].Value) : default(int?),
            DesiredHeight = grp["pichgoal"].Success ? int.Parse(grp["pichgoal"].Value) : default(int?),
        });
};

Syntax:

(\picw & \pich) \picwgoal? & \pichgoal? \picscalex? & \picscaley? & \picscaled? & \piccropt? & \piccropb? & \piccropr? & \piccropl?

-- Source: https://msdn.microsoft.com/en-us/library/aa140283(office.10).aspx

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • Thank you for your code, but how do I get DesiredWidth or DesiredHeight?Excuse me, I'm a beginner... – kfcalf Feb 15 '18 at 01:45