में चिपकाया गया है मेरे पास एक समृद्ध टेक्स्टबॉक्स है, जिसे मैं डेटाबेस में सहेजने की योजना बना रहा हूं, जिसे उसी समृद्ध टेक्स्टबॉक्स में वापस लोड किया जा सकता है। मुझे यह काम मिल गया है ताकि मैं flowdocument को DataFormats.XamlPackage के रूप में सहेज सकूं, जो छवियों को बचाता है, लेकिन मुद्दा यह है कि पाठ खोजने योग्य नहीं है। DataFormats.Xaml के साथ, मुझे पाठ्यक्रम का पाठ मिला है, लेकिन कोई छवि नहीं है। छवियों को अंतिम उपयोगकर्ता द्वारा चिपकाया जाएगा, न कि एप्लिकेशन के साथ शामिल छवियों।सी # डब्ल्यूपीएफ कन्वर्ट बिटमैप इमेज समृद्ध टेक्स्टबॉक्स में बाइनरी
मैंने XML में टेक्स्ट प्राप्त करने के लिए XamlWriter का उपयोग करने का प्रयास किया, और उसके बाद दस्तावेज़ से छवियों को अलग से पकड़ लिया और उन्हें XML में बाइनरी के रूप में डालें, लेकिन मुझे छवियों को बाइनरी में लाने का कोई तरीका नहीं दिख रहा है। ..
क्या किसी के पास विचार है कि छवियों को बाइनरी में कैसे प्राप्त किया जाए, पाठ से अलग?
अग्रिम धन्यवाद!
GetImageByteArray() वह मुद्दा है जहां समस्या है।
कोड:
private void SaveXML()
{
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
FlowDocument flowDocument = richTextBox.Document;
using (StringWriter stringwriter = new StringWriter())
{
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stringwriter))
{
XamlWriter.Save(flowDocument, writer);
}
testRTF t = new testRTF();
t.RtfText = new byte[0];
t.RtfXML = GetImagesXML(flowDocument);
t.RtfFullText = stringwriter.ToString();
//save t to database
}
richTextBox.Document.Blocks.Clear();
}
private string GetImagesXML(FlowDocument flowDocument)
{
using (StringWriter stringwriter = new StringWriter())
{
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stringwriter))
{
Type inlineType;
InlineUIContainer uic;
System.Windows.Controls.Image replacementImage;
byte[] bytes;
System.Text.ASCIIEncoding enc;
//loop through replacing images in the flowdoc with the byte versions
foreach (Block b in flowDocument.Blocks)
{
foreach (Inline i in ((Paragraph)b).Inlines)
{
inlineType = i.GetType();
if (inlineType == typeof(Run))
{
//The inline is TEXT!!!
}
else if (inlineType == typeof(InlineUIContainer))
{
//The inline has an object, likely an IMAGE!!!
uic = ((InlineUIContainer)i);
//if it is an image
if (uic.Child.GetType() == typeof(System.Windows.Controls.Image))
{
//grab the image
replacementImage = (System.Windows.Controls.Image)uic.Child;
//get its byte array
bytes = GetImageByteArray((BitmapImage)replacementImage.Source);
//write the element
writer.WriteStartElement("Image");
//put the bytes into the tag
enc = new System.Text.ASCIIEncoding();
writer.WriteString(enc.GetString(bytes));
//close the element
writer.WriteEndElement();
}
}
}
}
}
return stringwriter.ToString();
}
}
//This function is where the problem is, i need a way to get the byte array
private byte[] GetImageByteArray(BitmapImage bi)
{
byte[] result = new byte[0];
using (MemoryStream ms = new MemoryStream())
{
XamlWriter.Save(bi, ms);
//result = new byte[ms.Length];
result = ms.ToArray();
}
return result;
}
अद्यतन
मुझे लगता है कि मैं हो सकता है अंत में एक समाधान है, जो मैं नीचे पोस्ट करेंगे पाया। यह BmpBitmapEncoder और BmpBitmapDecoder का उपयोग करता है। यह मुझे बिटमैप छवि से बाइनरी प्राप्त करने की अनुमति देता है, इसे डेटाबेस में संग्रहीत करता है, और इसे बैक अप लोड करता है और उसे फ़्लो डॉक्यूमेंट में वापस प्रदर्शित करता है। प्रारंभिक परीक्षण सफल साबित हुए हैं। परीक्षण उद्देश्यों के लिए मैं अपने डेटाबेस चरण को छोड़ रहा हूं और मूल रूप से बाइनरी बनाकर छवि को डुप्लिकेट कर रहा हूं, फिर बाइनरी ले रहा हूं और इसे एक नई छवि में बदल रहा हूं और इसे फ़्लो डॉक्यूमेंट में जोड़ रहा हूं। एकमात्र मुद्दा यह है कि जब मैं संशोधित फ़्लो डॉक्यूमेंट को आज़माता हूं और लेता हूं और XamlWriter का उपयोग करता हूं। फ़ंक्शन सहेजें, यह नव निर्मित छवि पर "गैर-सार्वजनिक प्रकार को क्रमबद्ध नहीं कर सकता" सिस्टम के साथ त्रुटियां। Windows.Media.Imaging.BitmapFrameDecode "। यह कुछ और जांच लेगा। हालांकि मुझे इसे अभी अकेले छोड़ना होगा।
private void SaveXML()
{
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
FlowDocument flowDocument = richTextBox.Document;
string s = GetImagesXML(flowDocument);//temp
LoadImagesIntoXML(s);
using (StringWriter stringwriter = new StringWriter())
{
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stringwriter))
{
XamlWriter.Save(flowDocument, writer);//Throws error here
}
}
}
private string GetImagesXML(FlowDocument flowDocument)
{
string s= "";
using (StringWriter stringwriter = new StringWriter())
{
Type inlineType;
InlineUIContainer uic;
System.Windows.Controls.Image replacementImage;
byte[] bytes;
BitmapImage bi;
//loop through replacing images in the flowdoc with the byte versions
foreach (Block b in flowDocument.Blocks)
{
foreach (Inline i in ((Paragraph)b).Inlines)
{
inlineType = i.GetType();
if (inlineType == typeof(Run))
{
//The inline is TEXT!!!
}
else if (inlineType == typeof(InlineUIContainer))
{
//The inline has an object, likely an IMAGE!!!
uic = ((InlineUIContainer)i);
//if it is an image
if (uic.Child.GetType() == typeof(System.Windows.Controls.Image))
{
//grab the image
replacementImage = (System.Windows.Controls.Image)uic.Child;
bi = (BitmapImage)replacementImage.Source;
//get its byte array
bytes = GetImageByteArray(bi);
s = Convert.ToBase64String(bytes);//temp
}
}
}
}
return s;
}
}
private byte[] GetImageByteArray(BitmapImage src)
{
MemoryStream stream = new MemoryStream();
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)src));
encoder.Save(stream);
stream.Flush();
return stream.ToArray();
}
private void LoadImagesIntoXML(string xml)
{
byte[] imageArr = Convert.FromBase64String(xml);
System.Windows.Controls.Image img = new System.Windows.Controls.Image()
MemoryStream stream = new MemoryStream(imageArr);
BmpBitmapDecoder decoder = new BmpBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
img.Source = decoder.Frames[0];
img.Stretch = Stretch.None;
Paragraph p = new Paragraph();
p.Inlines.Add(img);
richTextBox.Document.Blocks.Add(p);
}
अधिक उपयुक्त कुछ करने के लिए इस पोस्ट का शीर्षक बदलने के लिए चाहते हो सकता है ... ऐसा लगता है तुम सच में कैसे एक छवि वस्तु के कच्चे बाइट्स लेने के लिए और एक XML दस्तावेज में यह स्टोर करने के लिए पूछ रहे हैं ... –
धन्यवाद , अच्छा लगता है, अगर किसी के पास बाइनरी में पूर्ण प्लस छवियों में टेक्स्ट को सहेजने के समान प्रभाव को प्राप्त करने के बारे में अन्य विचार हैं, तो मुझे संभवतः विशिष्ट होने की कोशिश कर रहा था, लेकिन मुझे नहीं लगता कि यह संभव है – JoeSharp