请问关于setpixel()画图?

lizilily 2004-08-30 04:14:35
我想用setpixel画图,我的程序编译通过,在执行时点击菜单但是却没有图画出来,为什么?下面是我得程序。
void CGxlView::OnMarking()
{
// TODO: Add your command handler code here
CWnd *pWnd=GetDlgItem(IDC_PICTURE_DISPLAY);//得到控件窗口的指针
CDC *pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();


CGxlDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

HDIB hDIB = pDoc->GetHDIB();
if (hDIB != NULL)
{
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
int Width=(int)::DIBWidth(lpDIB);
int Height=(int)::DIBHeight(lpDIB);
for(int i=1;i<Height;i++)
{
for(int j=1;i<Width;j++)
{
pDC->SetPixel(i,j,RGB(255,0,0));
}
}
}

pWnd->ReleaseDC(pDC);
}
...全文
278 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wrcluomo 2004-08-30
  • 打赏
  • 举报
回复
哈哈,楼上的精神可佳,这都能看出来.
zhaogaojian 2004-08-30
  • 打赏
  • 举报
回复
靠,害的我死了n次机,你的for(int j=1;i<Width;j++)写错了,应该是j<Width
wrcluomo 2004-08-30
  • 打赏
  • 举报
回复
pWnd->Invalidate();
pWnd->UpdateWindow();
这两句在作怪.你把他去掉试试.虽然你把他写在了画点这前,但在你画好后他还是给你清掉了.
DentistryDoctor 2004-08-30
  • 打赏
  • 举报
回复
显示代码应该写在OnDraw(对于视图类)中。
//得到随机字符串,长度自己定义 private string getRandomValidate(int len) { int num; int tem; string rtuStr = ""; for (int i = 0; i < len; i++) { num = ran.Next(); /* * 这里可以选择生成字符和数字组合的验证码 */ tem = num % 10 + '0';//生成数字 // tem = num % 26 + 'A';//生成字符 rtuStr += Convert.ToChar(tem).ToString(); } return rtuStr; } //生成图像 private void getImageValidate(string strValue) { //string str = "OO00"; //前两个为字母O,后两个为数字0 int width = Convert.ToInt32(strValue.Length * 12); //计算图像宽度 Bitmap img = new Bitmap(width, 23); Graphics gfc = Graphics.FromImage(img); //产生Graphics对象,进行画图 gfc.Clear(Color.White); drawLine(gfc, img); //写验证码,需要定义Font Font font = new Font("arial", 12, FontStyle.Bold); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true); gfc.DrawString(strValue, font, brush, 3, 2); drawPoint(img); gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1); //将图像添加到页面 MemoryStream ms = new MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); //更改Http头 Response.ClearContent(); Response.ContentType = "image/gif"; Response.BinaryWrite(ms.ToArray()); //Dispose gfc.Dispose(); img.Dispose(); Response.End(); } private void drawLine(Graphics gfc, Bitmap img) { //选择画10条线,也可以增加,也可以不要线,只要随机杂点即可 for (int i = 0; i < 10; i++) { int x1 = ran.Next(img.Width); int y1 = ran.Next(img.Height); int x2 = ran.Next(img.Width); int y2 = ran.Next(img.Height); gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //注意画笔一定要浅颜色,否则验证码看不清楚 } } private void drawPoint(Bitmap img) { /* //选择画100个点,可以根据实际情况改变 for (int i = 0; i < 100; i++) { int x = ran.Next(img.Width); int y = ran.Next(img.Height); img.SetPixel(x,y,Color.FromArgb(ran.Next()));//杂点颜色随机 }*/ int col = ran.Next();//在一次的图片中杂店颜色相同 for (int i = 0; i < 100; i++) { int x = ran.Next(img.Width); int y = ran.Next(img.Height); img.SetPixel(x, y, Color.FromArgb(col)); } }

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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