- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道有很多这样的问题,但我问是因为我无法理解给出的所有答案。我有 RichTextBox
,我希望用户能够在当前光标位置插入图像。
我试过使用Clipboard
设置图像,然后将其粘贴到富文本框中。这行得通,但有人告诉我这是不好的做法,因为它会在不通知用户的情况下更改剪贴板中的数据。
这是我试过的
private bool CheckIfImage(string filename)
{
if (filename.EndsWith(".jpeg")) { return true; }
else if (filename.EndsWith(".jpg")) { return true; }
else if (filename.EndsWith(".png")) { return true; }
else if (filename.EndsWith(".ico")) { return true; }
else if (filename.EndsWith(".gif")) { return true; }
else if (filename.EndsWith(".bmp")) { return true; }
else if (filename.EndsWith(".emp")) { return true; }
else if (filename.EndsWith(".wmf")) { return true; }
else if (filename.EndsWith(".tiff")) { return true; }
else { return false; }
}
private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
{
if (CheckIfImage(openFileDialog2.FileName.ToLower()) == true)
{
Image img = Image.FromFile(openFileDialog2.FileName);
string setData = (String)Clipboard.GetData(DataFormats.Rtf);
Clipboard.SetImage(img);
rtbType.Paste();
Clipboard.SetData(DataFormats.Rtf, setData);
}
else
{
MessageBox.Show("Invalid Image File Selected");
}
}
请问有什么更好的方法吗?
最佳答案
我已经使用发布的解决方案为您准备了一个功能齐全的示例 here利用 RTF 的力量。
正如 Hans Passant 所写:解决方案非常棘手,并且有一些有效的替代方案可以实现。
顺便说一句,这是你的代码(重写):
private bool CheckIfImage(string filename)
{
var valids = new[] {".jpeg", ".jpg", ".png", ".ico", ".gif", ".bmp", ".emp", ".wmf", ".tiff"};
return valids.Contains(System.IO.Path.GetExtension(filename));
}
private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
{
if (CheckIfImage(openFileDialog2.FileName.ToLower()) == true)
embedImage(Image.FromFile(openFileDialog2.FileName));
else
MessageBox.Show("Invalid Image File Selected");
}
这是embedImage
方法:
private void embedImage(Image img)
{
var rtf = new StringBuilder();
// Append the RTF header
rtf.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033");
// Create the font table using the RichTextBox's current font and append
// it to the RTF string
rtf.Append(GetFontTable(this.Font));
// Create the image control string and append it to the RTF string
rtf.Append(GetImagePrefix(img));
// Create the Windows Metafile and append its bytes in HEX format
rtf.Append(getRtfImage(img));
// Close the RTF image control string
rtf.Append(@"}");
richTextBox1.SelectedRtf = rtf.ToString();
}
这里有所有必要的方法:
private enum EmfToWmfBitsFlags
{
EmfToWmfBitsFlagsDefault = 0x00000000,
EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
EmfToWmfBitsFlagsNoXORClip = 0x00000004
};
private struct RtfFontFamilyDef
{
public const string Unknown = @"\fnil";
public const string Roman = @"\froman";
public const string Swiss = @"\fswiss";
public const string Modern = @"\fmodern";
public const string Script = @"\fscript";
public const string Decor = @"\fdecor";
public const string Technical = @"\ftech";
public const string BiDirect = @"\fbidi";
}
[DllImport("gdiplus.dll")]
private static extern uint GdipEmfToWmfBits(IntPtr _hEmf,
uint _bufferSize, byte[] _buffer,
int _mappingMode, EmfToWmfBitsFlags _flags);
private string GetFontTable(Font font)
{
var fontTable = new StringBuilder();
// Append table control string
fontTable.Append(@"{\fonttbl{\f0");
fontTable.Append(@"\");
var rtfFontFamily = new HybridDictionary();
rtfFontFamily.Add(FontFamily.GenericMonospace.Name, RtfFontFamilyDef.Modern);
rtfFontFamily.Add(FontFamily.GenericSansSerif, RtfFontFamilyDef.Swiss);
rtfFontFamily.Add(FontFamily.GenericSerif, RtfFontFamilyDef.Roman);
rtfFontFamily.Add("UNKNOWN", RtfFontFamilyDef.Unknown);
// If the font's family corresponds to an RTF family, append the
// RTF family name, else, append the RTF for unknown font family.
fontTable.Append(rtfFontFamily.Contains(font.FontFamily.Name) ? rtfFontFamily[font.FontFamily.Name] : rtfFontFamily["UNKNOWN"]);
// \fcharset specifies the character set of a font in the font table.
// 0 is for ANSI.
fontTable.Append(@"\fcharset0 ");
// Append the name of the font
fontTable.Append(font.Name);
// Close control string
fontTable.Append(@";}}");
return fontTable.ToString();
}
private string GetImagePrefix(Image _image)
{
float xDpi, yDpi;
var rtf = new StringBuilder();
using (Graphics graphics = CreateGraphics())
{
xDpi = graphics.DpiX;
yDpi = graphics.DpiY;
}
// Calculate the current width of the image in (0.01)mm
var picw = (int)Math.Round((_image.Width / xDpi) * 2540);
// Calculate the current height of the image in (0.01)mm
var pich = (int)Math.Round((_image.Height / yDpi) * 2540);
// Calculate the target width of the image in twips
var picwgoal = (int)Math.Round((_image.Width / xDpi) * 1440);
// Calculate the target height of the image in twips
var pichgoal = (int)Math.Round((_image.Height / yDpi) * 1440);
// Append values to RTF string
rtf.Append(@"{\pict\wmetafile8");
rtf.Append(@"\picw");
rtf.Append(picw);
rtf.Append(@"\pich");
rtf.Append(pich);
rtf.Append(@"\picwgoal");
rtf.Append(picwgoal);
rtf.Append(@"\pichgoal");
rtf.Append(pichgoal);
rtf.Append(" ");
return rtf.ToString();
}
private string getRtfImage(Image image)
{
// Used to store the enhanced metafile
MemoryStream stream = null;
// Used to create the metafile and draw the image
Graphics graphics = null;
// The enhanced metafile
Metafile metaFile = null;
try
{
var rtf = new StringBuilder();
stream = new MemoryStream();
// Get a graphics context from the RichTextBox
using (graphics = CreateGraphics())
{
// Get the device context from the graphics context
IntPtr hdc = graphics.GetHdc();
// Create a new Enhanced Metafile from the device context
metaFile = new Metafile(stream, hdc);
// Release the device context
graphics.ReleaseHdc(hdc);
}
// Get a graphics context from the Enhanced Metafile
using (graphics = Graphics.FromImage(metaFile))
{
// Draw the image on the Enhanced Metafile
graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
}
// Get the handle of the Enhanced Metafile
IntPtr hEmf = metaFile.GetHenhmetafile();
// A call to EmfToWmfBits with a null buffer return the size of the
// buffer need to store the WMF bits. Use this to get the buffer
// size.
uint bufferSize = GdipEmfToWmfBits(hEmf, 0, null, 8, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
// Create an array to hold the bits
var buffer = new byte[bufferSize];
// A call to EmfToWmfBits with a valid buffer copies the bits into the
// buffer an returns the number of bits in the WMF.
uint _convertedSize = GdipEmfToWmfBits(hEmf, bufferSize, buffer, 8, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
// Append the bits to the RTF string
foreach (byte t in buffer)
{
rtf.Append(String.Format("{0:X2}", t));
}
return rtf.ToString();
}
finally
{
if (graphics != null)
graphics.Dispose();
if (metaFile != null)
metaFile.Dispose();
if (stream != null)
stream.Close();
}
}
我建议您将其包装到您自己的 UserControl
中。
关于c# - 在富文本框中的光标位置插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18017044/
我的意思是这三个富人的论点: #{rich:clientId('id')} #{rich:element('id')} #{rich:component('id')} 例如在这些方法调用(action
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What are the differences between pointer variable and
对于实习,我将不得不开发一个桌面应用程序。重点是创建丰富的 UI(酷炫的效果、声音等)。我应该使用哪种技术? - 闪光 ? (在这种情况下,我应该使用 flex 项目吗?AIR ?这和一个简单的 ra
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我想知道这一行的作用: foo || (foo = this.foo) 在下面的函数定义中 someFunction: function(foo) { foo || (foo = thi
我有一个数据模型,其中有一个 Item可以属于一个或多个 Categories .我正在尝试显示 允许用户选择哪个Categories Item应该属于。我正在使用 RichFaces 和 Seam
我意识到我大部分时间都在使用三元运算符,如下所示: foo ? foo:酒吧; 这变得很麻烦,因为变量长度变得很长,例如。克。 appModel.settings.notifications ? ap
我要做出一个设计决定。 在我的 web(ajax) 应用程序中,我们需要决定将用户界面逻辑放在哪里? 是否应该通过 javascript 完全加载(纯单页)。只有数据来来去去。 或 服务器是否应该发送
有人向我指出我在某些 C++ 代码中有一个看起来像拼写错误的地方: protected: Foo x, y,; 我原以为结尾的逗号会出错,但显然不是?这是未定义的,还是发生了什么?大概是坏事
谁能解释一下这是做什么的? var foo = foo || alert(foo); 最佳答案 如果 foo 已经被定义并且计算结果为真,它设置 foo = foo,即它什么都不做。 如果定义了 fo
看起来这应该是可能的,但是......? 使用 richfaces 和 JSF,我正在使用 rich:dataList 遍历 List ...一切都很好,除了我希望能够选择性地“呈现”每次迭代,这可能
我有以下带有 rich:calendar 的 xhtml 文件,我正试图禁用一些日子 using this example .但是永远不会调用 javascript 函数。我不知道为什么。
我的应用程序在 90% 的时间里都使用 Ajax 调用来获取后端数据。我需要为所有这些调用实现 CSRF 预防。所以我需要在每次调用时传递 token 。我在哪里生成 token ?在客户端还是服务器
我正在尝试重构和引入一些旧代码,我遇到了这样的事情: struct foo; typedef struct foo * foo; 尝试编译它时,出现以下错误: Sourc
我想使用类似 TextView 的东西进行类似串行/TTY/文本终端的同步用户交互。我的意思是我需要以下操作: 在末尾添加一些新文本(使用各种 Span 设置样式) 同步等待用户在最后输入一些进一步的
片段 1: if ( x ) { foo(); } 片段 2: x ? foo() : 0; 这两个片段之间有什么区别? 编辑:更正了语法错误。 更新:顺便说一句,似乎还有一个更短的符号:
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 7 年前。 我的类实现了一个非常简单的 RPN 计算器原型(prototype)。
我正在我的元素中使用,我需要为其自定义 CSS。我想在文件上传按钮中删除“+”标记(参见图片),我该怎么做? 提前致谢。 最佳答案 只是覆盖样式,按钮类是rf-fu-btn-cnt-add,加号是背景
我在 jsf .xhtml 页面中有一个 richdatatable。我有一个特定单元格的很长的文本。我如何设置列的宽度并在指定的单元格中显示文本。我是 JSF 和 CSS 的新手。我正在提供一个例子
我是一名优秀的程序员,十分优秀!