请问大家,如何用c#保存richtextbox中的图片?

daixiaoxiao 2009-05-11 09:38:15
如题,我现在想要把其中的图片存出来。找过网上的一些例子,但都是c或者c++的,看不懂。

恳求各位达人,能不能给我个思路。

或者将richtextbox整个截成一张图也行。要能把滚动条下的内容全部截到一起,而不是只截取看得见的内容。

先谢谢大家了
...全文
2125 49 打赏 收藏 转发到动态 举报
写回复
用AI写文章
49 条回复
切换为时间正序
请发表友善的回复…
发表回复
penghuihenan 2011-11-12
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 hikaliv 的回复:]
public static Bitmap RtbToBitmap(RichTextBox rtb) {
rtb.Update(); // Ensure RTB fully painted
Bitmap bmp = new Bitmap(rtb.Width, rtb.Height);
using (Graphics gr = Graphics.FromImage(bmp)) {
……
[/Quote]


这种方法能用,哥啊,我太崇拜你了,找了好久,终于找到了啊
yanfuliang 2011-11-08
  • 打赏
  • 举报
回复
没有结果 继续等待...
yanfuliang 2011-11-08
  • 打赏
  • 举报
回复
一直在关注
6742 2010-10-27
  • 打赏
  • 举报
回复
关注,怎么会没有人解决这个问题?
JiYi512 2009-12-17
  • 打赏
  • 举报
回复
问题还是没解决,答案全看了一遍,怎么没有能解决楼主的问题呢?我遇到的问题一样,帮顶下,继续关注
tongxin1105 2009-08-15
  • 打赏
  • 举报
回复
tongxin1105 2009-08-15
  • 打赏
  • 举报
回复
[del][/del]
junweishiwo 2009-05-11
  • 打赏
  • 举报
回复
不懂,帮顶!
daixiaoxiao 2009-05-11
  • 打赏
  • 举报
回复
谢谢 ximi82878
我现在就是想要这张图,请问该如何做转换呢?
daixiaoxiao 2009-05-11
  • 打赏
  • 举报
回复
public int Print(int charFrom, int charTo, PrintPageEventArgs e)
{
//Calculate the area to render and print
RECT rectToPrint;
rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);

//Calculate the size of the page
RECT rectPage;
rectPage.Top = (int)(e.PageBounds.Top * anInch);
rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
rectPage.Left = (int)(e.PageBounds.Left * anInch);
rectPage.Right = (int)(e.PageBounds.Right * anInch);

IntPtr hdc = e.Graphics.GetHdc();

FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = charTo; //Indicate character from to character to
fmtRange.chrg.cpMin = charFrom;
fmtRange.hdc = hdc; //Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc; //Point at printer hDC
fmtRange.rc = rectToPrint; //Indicate the area on page to print
fmtRange.rcPage = rectPage; //Indicate size of page

IntPtr res = IntPtr.Zero;

IntPtr wparam = IntPtr.Zero;
wparam = new IntPtr(1);

//Get the pointer to the FORMATRANGE structure in memory
IntPtr lparam = IntPtr.Zero;
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lparam, false);

//Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);

//Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam);
//Release the device context handle obtained by a previous call

e.Graphics.ReleaseHdc(hdc);


//Return last + 1 character printer
return res.ToInt32();
}
ximi82878 2009-05-11
  • 打赏
  • 举报
回复
你可以做个转换啊,存完打印,变通一下。。。
daixiaoxiao 2009-05-11
  • 打赏
  • 举报
回复
第二个是图片的问题,这也是本文的重点。RTF中图片以两种方式存在:第一种方式是直接嵌入,以{\pict开始;第二种方式是作为OLE对象嵌入,这时以{\object开始。当RTF处理器能直接使用OLE时,RTF文件中提供了OLE的数据;否则,文件中直接提供图片的数据,以{\result开始。在使用中最常见的图片格式是内含DIB BITMAP的元文件(METAFILE),这种格式在SDK中没有说明,而且在RTF中是以压缩形式储存的,所以在转换时有一定困难。我们采取了一种比较新的方法:先把META FILE的数据读出来存成一个文件,然后用GetMetafile和PlayMetafile函数把文件中的图形打印在一个内存DC上,最后用抓图的方法把它存成BITMAP文件。以下是具体程序:
daixiaoxiao 2009-05-11
  • 打赏
  • 举报
回复


  void WmfToBmp(int width,int height,char *metafile)

  {

  //width为图片宽度,height为图片高度,*metafile为元文件名

  static HMETAFILE hmf;

  char num[4];

  FILE *fh;

  HDC hMemDC;

  HBITMAP hbmp,hold;

  BITMAPFILEHEADER hdr;

  LPBITMAPINFOHEADER lpbmpih;

  HGLOBAL hg;

  

  BitmapCount++;

  if(BitmapCount==3)

  {

  width=128;

  height=132;

  }

  if(BitmapCount==1)

  {

  width=80;

  height=50;

  }

  ToString10(BitmapCount,num);

  //把字符串转换成数字的函数

  lstrcpy(bmpFile,FilePath);

  lstrcat(bmpFile,num);

  lstrcpy(gifFile,bmpFile);

  lstrcat(bmpFile,".bmp");

  lstrcat(gifFile,".gif");

  hmf=GetMetaFile(metafile);

  hDisplayDC=CreateDC("DISPLAY",NULL,NULL,NULL);

  hMemDC=CreateCompatibleDC(hDisplayDC);

  hg=GlobalAlloc(GHND,sizeof(BITMAPINFOHEADER));

  lpbmpih=(LPBITMAPINFOHEADER)GlobalLock(hg);

  lpbmpih->biSize=sizeof(BITMAPINFOHEADER);

  lpbmpih->biWidth=width;

  lpbmpih->biHeight=height;

  lpbmpih->biPlanes=1;

  lpbmpih->biBitCount=8;

  lpbmpih->biCompression=0;

  hbmp=CreateCompatibleBitmap(hDisplayDC,width,height);

  GlobalUnlock(hg);

  GlobalFree(hg);

  hold=SelectObject(hMemDC,hbmp);

  SetMapMode(hMemDC,MM_ANISOTROPIC);

  SetWindowOrgEx(hMemDC,0,0,NULL);

  SetViewportExtEx(hMemDC,width,height,NULL);

  PlayMetaFile(hMemDC,hmf);

  hbmp=SelectObject(hMemDC,hold);

  if((fh=fopen(bmpFile,"w+b"))==NULL)

  {

  return;

  }

  hdr.bfType=0x4d42;

  hdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+width*height;

  hdr.bfReserved1=0;

  hdr.bfReserved2=0;

  hdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);

  fwrite((LPSTR)&hdr,sizeof(BITMAPFILEHEADER),1,fh);

  hg=GlobalAlloc(GHND,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD));

  lpbmpih=(LPBITMAPINFOHEADER)GlobalLock(hg);

  lpbmpih->biSize=sizeof(BITMAPINFOHEADER);

  lpbmpih->biWidth=width;

  lpbmpih->biHeight=height;

  lpbmpih->biPlanes=1;

  lpbmpih->biBitCount=8;

  lpbmpih->biCompression=0;

  lpbmpih->biSizeImage=width*height;

  lpbmpih->biClrUsed=256;

  lpbmpih->biClrImportant=0;

  j=GetDIBits(hMemDC,hbmp,0,height,NULL,(BITMAPINFO*)lpbmpih,DIB_RGB_COLORS);

  lpbmpih->biSize=sizeof(BITMAPINFOHEADER);

  fwrite(lpbmpih,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD),1,fh);

  GetDIBits(hMemDC,hbmp,0,height,(LPSTR)FileBuffer,(BITMAPINFO*)lpbmpih,DIB_RGB_COLS);

  fwrite(FileBuffer,width*height,1,fh);

  GlobalUnlock(hg);

  GlobalFree(hg);

  fclose(fh);

  DeleteDC(hMemDC);

  DeleteDC(hDisplayDC);

  DeleteObject(hbmp);

  DeleteObject(hold);

  DeleteMetaFile(hmf);

  }//end function WmfToBmp()


daixiaoxiao 2009-05-11
  • 打赏
  • 举报
回复
谢谢zgke
但是现在问题是。不能直接打印出来,要把这个内容,存成一张图片。
zgke 2009-05-11
  • 打赏
  • 举报
回复
daixiaoxiao 2009-05-11
  • 打赏
  • 举报
回复
谢谢帮顶~
goldxinx 2009-05-11
  • 打赏
  • 举报
回复
关注
llsen 2009-05-11
  • 打赏
  • 举报
回复
sf
delacrxoix_xu 2009-05-11
  • 打赏
  • 举报
回复
用 picturebox吧! 我就用的这个 变通一下 干嘛非得吊死在一棵 歪脖树上

pictureBox1.Image.Save(Filename);就搞定了!
zhushoudong 2009-05-11
  • 打赏
  • 举报
回复
怎么用到 句柄了的 顶 private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "图片文件|*.jpg|所有文件|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Clipboard.SetDataObject(Image.FromFile(openFileDialog1.FileName), false);
richTextBox1.Paste();
}
}
加载更多回复(29)

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧