100分求一个在richtextbox插入图片的方法.不能用粘贴.找到答案马上结帖!

时间:2022-05-12 21:40:32
在richtextbox中插入图片,但是不能用复制粘贴这种方法,因为这种方法会引起图片失真.
我需要的是在本地硬盘里选择图片插入.
找到方法,立刻结帖.
当然,如果提供多文本编辑控件也行.谢谢.

16 个解决方案

#1


用Lionsky HtmlEditor控件
http://www.lionsky.net

#2


在vb里是这样用的
使用剪贴板的方法:  
Private  Declare  Function  SendMessage  Lib  "user32"  Alias  "SendMessageA"  (ByVal  hwnd  As  Long,  ByVal  wMsg  As  Long,  ByVal  wParam  As  Long,  lParam  As  Any)  As  Long
PicCopy.Picture  =  LoadPicture("C:=\aa.bmp")  
Clipboard.SetData  PicCopy.Image  '将图形内容拷贝到剪贴板  
SendMessage  Richtextbox1.hwnd,  WM_PASTE,  0,  0  '插入到RICHTEXTBOX控件中

#3


重申一下:是winform不是aspx,
第二不能使用粘贴这种方法,这会让图片失真.

#4


up

#5


上去.

#6


帮你up

#7


双星,你帮我回答阿.

#8


up

#9


#10


谢谢,我再UP.

#11


不会失真的 关键是你copy的时候要选对图片格式

我写过一个程序 用来保存文档 连《七剑》的海报都存进去了 巨清晰

@_@

#12


http://support.microsoft.com/default.aspx?scid=kb;en-us;220844

#13


真的会失真,
加入是个四方形的图片看不出来,
如果是一个圆形的图片的话,会在后面加一个背景呀,如何解决?

#14


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
public class ContextAdder
{
public const int
EmfToWmfBitsFlagsDefault = 0x00000000,
EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
EmfToWmfBitsFlagsNoXORClip = 0x00000004;

[DllImport("gdiplus.dll")]
public static extern uint GdipEmfToWmfBits(IntPtr _hEmf, uint _bufferSize, byte[] _buffer, int _mappingMode, int _flags);

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";
}

private const int MM_ISOTROPIC = 7;
private const int MM_ANISOTROPIC = 8;
private const int HMM_PER_INCH = 2540;
private const int TWIPS_PER_INCH = 1440;

private const string FF_UNKNOWN = "UNKNOWN";

private const string RTF_HEADER = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033";
private const string RTF_DOCUMENT_PRE = @"\viewkind4\uc1\pard\cf1\f0\fs20";
private const string RTF_DOCUMENT_POST = @"\cf0\fs17}";
private const string RTF_IMAGE_POST = @"}";

private static HybridDictionary rtfFontFamily;

static ContextAdder()
{
rtfFontFamily = new HybridDictionary();
rtfFontFamily.Add(FontFamily.GenericMonospace.Name, RtfFontFamilyDef.Modern);
rtfFontFamily.Add(FontFamily.GenericSansSerif.Name, RtfFontFamilyDef.Swiss);
rtfFontFamily.Add(FontFamily.GenericSerif.Name, RtfFontFamilyDef.Roman);
rtfFontFamily.Add(FF_UNKNOWN, RtfFontFamilyDef.Unknown);
}

private static string GetFontTable(Font _font) 
{
StringBuilder _fontTable = new StringBuilder();
// Append table control string
_fontTable.Append(@"{\fonttbl{\f0");
_fontTable.Append(@"\");
// If the font's family corresponds to an RTF family, append the
// RTF family name, else, append the RTF for unknown font family.
if (rtfFontFamily.Contains(_font.FontFamily.Name))
_fontTable.Append(rtfFontFamily[_font.FontFamily.Name]);
else
_fontTable.Append(rtfFontFamily[FF_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();
}

/// <summary>
/// 在RichTextBox当前光标处插入一副图像。
/// </summary>
/// <param name="rtb">多格式文本框控件</param>
/// <param name="image">插入的图像</param>
public static void InsertImage(RichTextBox rtb, Image image)
{
StringBuilder _rtf = new StringBuilder();
// Append the RTF header
_rtf.Append(RTF_HEADER);
// Create the font table using the RichTextBox's current font and append
// it to the RTF string
_rtf.Append(GetFontTable(rtb.Font));
// Create the image control string and append it to the RTF string
_rtf.Append(GetImagePrefix(rtb, image));
// Create the Windows Metafile and append its bytes in HEX format
_rtf.Append(GetRtfImage(rtb, image));
// Close the RTF image control string
_rtf.Append(RTF_IMAGE_POST);
rtb.SelectedRtf = _rtf.ToString();
}

private static string GetImagePrefix(RichTextBox rtb, Image _image) 
{
float xDpi;
float yDpi;
using(Graphics _graphics = rtb.CreateGraphics()) 
{
xDpi = _graphics.DpiX;
yDpi = _graphics.DpiY;
}

StringBuilder _rtf = new StringBuilder();
// Calculate the current width of the image in (0.01)mm
int picw = (int)Math.Round((_image.Width / xDpi) * HMM_PER_INCH);
// Calculate the current height of the image in (0.01)mm
int pich = (int)Math.Round((_image.Height / yDpi) * HMM_PER_INCH);
// Calculate the target width of the image in twips
int picwgoal = (int)Math.Round((_image.Width / xDpi) * TWIPS_PER_INCH);
// Calculate the target height of the image in twips
int pichgoal = (int)Math.Round((_image.Height / yDpi) * TWIPS_PER_INCH);
// 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 static string GetRtfImage(RichTextBox rtb, Image _image) 
{
StringBuilder _rtf = null;
// 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;
// Handle to the device context used to create the metafile
IntPtr _hdc;
try 
{
_rtf = new StringBuilder();
_stream = new MemoryStream();
// Get a graphics context from the RichTextBox
using(_graphics = rtb.CreateGraphics()) 
{
// Get the device context from the graphics context
_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, MM_ANISOTROPIC, EmfToWmfBitsFlagsDefault);
// Create an array to hold the bits
byte[] _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, MM_ANISOTROPIC, EmfToWmfBitsFlagsDefault);
// Append the bits to the RTF string
for(int i = 0; i < _buffer.Length; ++i) 
{
_rtf.Append(String.Format("{0:X2}", _buffer[i]));
}

return _rtf.ToString();
}
finally 
{
if(_graphics != null)
_graphics.Dispose();
if(_metaFile != null)
_metaFile.Dispose();
if(_stream != null)
_stream.Close();
}
}

public static void AppendContext(RichTextBox rtb,string context)
{
switch(context)
{
case "/sm":
System.Drawing.Bitmap img = new System.Drawing.Bitmap("smile.gif");
InsertImage(rtb,img);
break;
case "/ag":
System.Drawing.Bitmap img2 = new System.Drawing.Bitmap("angry.gif");
InsertImage(rtb,img2);
break;
default:
rtb.AppendText(context);
break;
}
}
}

#15


你说的那个不叫失真,是因为图像都是矩形的,你的圆形图片需要消掉四周的背景。
这恐怕对RichTextBox太难为了。因为现在做这种效果都要自己处理图像,使用GDI或者GDI+。

另外,以后能不能把问题说清楚一点,不要让大家回复了这么一大堆,才把问题说到点子上。

#16


结贴拉.

#1


用Lionsky HtmlEditor控件
http://www.lionsky.net

#2


在vb里是这样用的
使用剪贴板的方法:  
Private  Declare  Function  SendMessage  Lib  "user32"  Alias  "SendMessageA"  (ByVal  hwnd  As  Long,  ByVal  wMsg  As  Long,  ByVal  wParam  As  Long,  lParam  As  Any)  As  Long
PicCopy.Picture  =  LoadPicture("C:=\aa.bmp")  
Clipboard.SetData  PicCopy.Image  '将图形内容拷贝到剪贴板  
SendMessage  Richtextbox1.hwnd,  WM_PASTE,  0,  0  '插入到RICHTEXTBOX控件中

#3


重申一下:是winform不是aspx,
第二不能使用粘贴这种方法,这会让图片失真.

#4


up

#5


上去.

#6


帮你up

#7


双星,你帮我回答阿.

#8


up

#9


#10


谢谢,我再UP.

#11


不会失真的 关键是你copy的时候要选对图片格式

我写过一个程序 用来保存文档 连《七剑》的海报都存进去了 巨清晰

@_@

#12


http://support.microsoft.com/default.aspx?scid=kb;en-us;220844

#13


真的会失真,
加入是个四方形的图片看不出来,
如果是一个圆形的图片的话,会在后面加一个背景呀,如何解决?

#14


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
public class ContextAdder
{
public const int
EmfToWmfBitsFlagsDefault = 0x00000000,
EmfToWmfBitsFlagsEmbedEmf = 0x00000001,
EmfToWmfBitsFlagsIncludePlaceable = 0x00000002,
EmfToWmfBitsFlagsNoXORClip = 0x00000004;

[DllImport("gdiplus.dll")]
public static extern uint GdipEmfToWmfBits(IntPtr _hEmf, uint _bufferSize, byte[] _buffer, int _mappingMode, int _flags);

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";
}

private const int MM_ISOTROPIC = 7;
private const int MM_ANISOTROPIC = 8;
private const int HMM_PER_INCH = 2540;
private const int TWIPS_PER_INCH = 1440;

private const string FF_UNKNOWN = "UNKNOWN";

private const string RTF_HEADER = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033";
private const string RTF_DOCUMENT_PRE = @"\viewkind4\uc1\pard\cf1\f0\fs20";
private const string RTF_DOCUMENT_POST = @"\cf0\fs17}";
private const string RTF_IMAGE_POST = @"}";

private static HybridDictionary rtfFontFamily;

static ContextAdder()
{
rtfFontFamily = new HybridDictionary();
rtfFontFamily.Add(FontFamily.GenericMonospace.Name, RtfFontFamilyDef.Modern);
rtfFontFamily.Add(FontFamily.GenericSansSerif.Name, RtfFontFamilyDef.Swiss);
rtfFontFamily.Add(FontFamily.GenericSerif.Name, RtfFontFamilyDef.Roman);
rtfFontFamily.Add(FF_UNKNOWN, RtfFontFamilyDef.Unknown);
}

private static string GetFontTable(Font _font) 
{
StringBuilder _fontTable = new StringBuilder();
// Append table control string
_fontTable.Append(@"{\fonttbl{\f0");
_fontTable.Append(@"\");
// If the font's family corresponds to an RTF family, append the
// RTF family name, else, append the RTF for unknown font family.
if (rtfFontFamily.Contains(_font.FontFamily.Name))
_fontTable.Append(rtfFontFamily[_font.FontFamily.Name]);
else
_fontTable.Append(rtfFontFamily[FF_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();
}

/// <summary>
/// 在RichTextBox当前光标处插入一副图像。
/// </summary>
/// <param name="rtb">多格式文本框控件</param>
/// <param name="image">插入的图像</param>
public static void InsertImage(RichTextBox rtb, Image image)
{
StringBuilder _rtf = new StringBuilder();
// Append the RTF header
_rtf.Append(RTF_HEADER);
// Create the font table using the RichTextBox's current font and append
// it to the RTF string
_rtf.Append(GetFontTable(rtb.Font));
// Create the image control string and append it to the RTF string
_rtf.Append(GetImagePrefix(rtb, image));
// Create the Windows Metafile and append its bytes in HEX format
_rtf.Append(GetRtfImage(rtb, image));
// Close the RTF image control string
_rtf.Append(RTF_IMAGE_POST);
rtb.SelectedRtf = _rtf.ToString();
}

private static string GetImagePrefix(RichTextBox rtb, Image _image) 
{
float xDpi;
float yDpi;
using(Graphics _graphics = rtb.CreateGraphics()) 
{
xDpi = _graphics.DpiX;
yDpi = _graphics.DpiY;
}

StringBuilder _rtf = new StringBuilder();
// Calculate the current width of the image in (0.01)mm
int picw = (int)Math.Round((_image.Width / xDpi) * HMM_PER_INCH);
// Calculate the current height of the image in (0.01)mm
int pich = (int)Math.Round((_image.Height / yDpi) * HMM_PER_INCH);
// Calculate the target width of the image in twips
int picwgoal = (int)Math.Round((_image.Width / xDpi) * TWIPS_PER_INCH);
// Calculate the target height of the image in twips
int pichgoal = (int)Math.Round((_image.Height / yDpi) * TWIPS_PER_INCH);
// 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 static string GetRtfImage(RichTextBox rtb, Image _image) 
{
StringBuilder _rtf = null;
// 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;
// Handle to the device context used to create the metafile
IntPtr _hdc;
try 
{
_rtf = new StringBuilder();
_stream = new MemoryStream();
// Get a graphics context from the RichTextBox
using(_graphics = rtb.CreateGraphics()) 
{
// Get the device context from the graphics context
_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, MM_ANISOTROPIC, EmfToWmfBitsFlagsDefault);
// Create an array to hold the bits
byte[] _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, MM_ANISOTROPIC, EmfToWmfBitsFlagsDefault);
// Append the bits to the RTF string
for(int i = 0; i < _buffer.Length; ++i) 
{
_rtf.Append(String.Format("{0:X2}", _buffer[i]));
}

return _rtf.ToString();
}
finally 
{
if(_graphics != null)
_graphics.Dispose();
if(_metaFile != null)
_metaFile.Dispose();
if(_stream != null)
_stream.Close();
}
}

public static void AppendContext(RichTextBox rtb,string context)
{
switch(context)
{
case "/sm":
System.Drawing.Bitmap img = new System.Drawing.Bitmap("smile.gif");
InsertImage(rtb,img);
break;
case "/ag":
System.Drawing.Bitmap img2 = new System.Drawing.Bitmap("angry.gif");
InsertImage(rtb,img2);
break;
default:
rtb.AppendText(context);
break;
}
}
}

#15


你说的那个不叫失真,是因为图像都是矩形的,你的圆形图片需要消掉四周的背景。
这恐怕对RichTextBox太难为了。因为现在做这种效果都要自己处理图像,使用GDI或者GDI+。

另外,以后能不能把问题说清楚一点,不要让大家回复了这么一大堆,才把问题说到点子上。

#16


结贴拉.