Tôi có một richtextbox, mà tôi dự định lưu vào cơ sở dữ liệu, có thể được tải lại vào cùng một richtextbox. Tôi đã có nó làm việc để tôi có thể lưu flowdocument như DataFormats.XamlPackage, mà lưu hình ảnh, nhưng vấn đề là văn bản không thể tìm kiếm được. Với DataFormats.Xaml, tôi đã có văn bản tất nhiên, nhưng không có hình ảnh. Các hình ảnh sẽ được dán bởi người dùng cuối, không phải hình ảnh đi kèm với ứng dụng.C# WPF chuyển đổi BitmapImage được dán vào richtextbox thành dạng nhị phân
Tôi đã thử sử dụng XamlWriter để lấy văn bản thành XML, sau đó lấy các hình ảnh từ tài liệu một cách riêng biệt và chèn chúng dưới dạng nhị phân vào XML, nhưng dường như tôi không tìm được cách để đưa hình ảnh vào nhị phân. ..
Có ai có ý tưởng về cách đưa hình ảnh vào nhị phân, tách biệt với văn bản không?
Cảm ơn trước!
GetImageByteArray() là nơi xảy ra sự cố.
Code:
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;
}
CẬP NHẬT
Tôi nghĩ rằng tôi có thể cuối cùng đã tìm thấy một giải pháp, mà tôi sẽ đăng dưới đây. Nó sử dụng BmpBitmapEncoder và BmpBitmapDecoder. Điều này cho phép tôi lấy nhị phân từ hình ảnh bitmap, lưu nó vào cơ sở dữ liệu, và tải nó trở lại và hiển thị nó trở lại vào FlowDocument. Các thử nghiệm ban đầu đã chứng minh thành công. Đối với mục đích thử nghiệm, tôi bỏ qua bước cơ sở dữ liệu của mình và sao chép cơ bản hình ảnh bằng cách tạo nhị phân, sau đó lấy nhị phân và biến nó thành một hình ảnh mới và thêm nó vào FlowDocument. Vấn đề duy nhất là khi tôi thử và lấy FlowDocument đã sửa đổi và sử dụng hàm XamlWriter.Save, nó lỗi trên Image vừa tạo với "Không thể tuần tự hóa một kiểu không công khai" System.Windows.Media.Imaging.BitmapFrameDecode ". Điều này sẽ mất một số điều tra thêm. Tôi sẽ phải để nó một mình cho đến bây giờ.
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);
}
có thể muốn thay đổi tiêu đề bài đăng này thành nội dung phù hợp hơn ... có vẻ như bạn đang thực sự hỏi cách lấy byte thô của đối tượng Hình ảnh và lưu trữ trong tài liệu Xml ... –
cảm ơn Nghe có vẻ tốt, tôi đã cố gắng cụ thể nhất có thể trong trường hợp ai đó có ý tưởng khác về cách đạt được hiệu quả tương tự của việc lưu văn bản trong hình ảnh cộng đầy đủ trong nhị phân, nhưng tôi không nghĩ đó là khả năng – JoeSharp