DrawLine(int StartX, int EndX, int StartY, int EndY, RGBQUAD color, bool bSetAlpha)
{
if (!pDib) return;
//////////////////////////////////////////////////////
// Draws a line using the Bresenham line algorithm
// Thanks to Jordan DeLozier <JDL>
//////////////////////////////////////////////////////
int x1 = StartX;
int y1 = StartY;
int x = x1;
int y = y1;
int x2 = EndX;
int y2 = EndY; int xinc1,xinc2,yinc1,yinc2;
int den, num, numadd,numpixels;
int deltax = abs(x2 - x1);
int deltay = abs(y2 - y1); // Get Increasing Values
if (x2 >= x1) {
xinc1 = 1;
xinc2 = 1;
} else {
xinc1 = -1;
xinc2 = -1;
} if (y2 >= y1) {
yinc1 = 1;
yinc2 = 1;
} else {
yinc1 = -1;
yinc2 = -1;
} // Actually draw the line
if (deltax >= deltay)
{
xinc1 = 0;
yinc2 = 0;
den = deltax;
num = deltax / 2;
numadd = deltay;
numpixels = deltax;
}
else
{
xinc2 = 0;
yinc1 = 0;
den = deltay;
num = deltay / 2;
numadd = deltax;
numpixels = deltay;
}
for (int curpixel = 0; curpixel <= numpixels; curpixel++)
{
// Draw the current pixel
SetPixelColor(x,y,color,bSetAlpha);
num += numadd;
if (num >= den)
{
num -= den;
x += xinc1;
y += yinc1;
}
x += xinc2;
y += yinc2;
}
}
我知道是用的Bresenham画的 但是好像是用画点画上去了 这样就2个问题 一个就是画出来的锯齿很厉害 还一个我要是想画虚线就没办法了 也没办法设置线的粗细下面是画点的
SetPixelColor(long x,long y,RGBQUAD c, bool bSetAlpha)
{
if ((pDib==NULL)||(x<0)||(y<0)||
(x>=head.biWidth)||(y>=head.biHeight)) return;
if (head.biClrUsed)
BlindSetPixelIndex(x,y,GetNearestIndex(c));
else {
BYTE* iDst = info.pImage + y*info.dwEffWidth + x*3;
*iDst++ = c.rgbBlue;
*iDst++ = c.rgbGreen;
*iDst = c.rgbRed;
}void BlindSetPixelIndex(long x,long y,BYTE i)
{
#ifdef _DEBUG
if ((pDib==NULL)||(head.biClrUsed==0)||
(x<0)||(y<0)||(x>=head.biWidth)||(y>=head.biHeight))
#if CXIMAGE_SUPPORT_EXCEPTION_HANDLING
throw 0;
#else
return;
#endif
#endif if (head.biBitCount==8){
info.pImage[y*info.dwEffWidth + x]=i;
return;
} else {
BYTE pos;
BYTE* iDst= info.pImage + y*info.dwEffWidth + (x*head.biBitCount >> 3);
if (head.biBitCount==4){
pos = (BYTE)(4*(1-x%2));
*iDst &= ~(0x0F<<pos);
*iDst |= ((i & 0x0F)<<pos);
return;
} else if (head.biBitCount==1){
pos = (BYTE)(7-x%8);
*iDst &= ~(0x01<<pos);
*iDst |= ((i & 0x01)<<pos);
return;
}
}
}求高人指点啊 小弟谢谢了
{
if (!pDib) return;
//////////////////////////////////////////////////////
// Draws a line using the Bresenham line algorithm
// Thanks to Jordan DeLozier <JDL>
//////////////////////////////////////////////////////
int x1 = StartX;
int y1 = StartY;
int x = x1;
int y = y1;
int x2 = EndX;
int y2 = EndY; int xinc1,xinc2,yinc1,yinc2;
int den, num, numadd,numpixels;
int deltax = abs(x2 - x1);
int deltay = abs(y2 - y1); // Get Increasing Values
if (x2 >= x1) {
xinc1 = 1;
xinc2 = 1;
} else {
xinc1 = -1;
xinc2 = -1;
} if (y2 >= y1) {
yinc1 = 1;
yinc2 = 1;
} else {
yinc1 = -1;
yinc2 = -1;
} // Actually draw the line
if (deltax >= deltay)
{
xinc1 = 0;
yinc2 = 0;
den = deltax;
num = deltax / 2;
numadd = deltay;
numpixels = deltax;
}
else
{
xinc2 = 0;
yinc1 = 0;
den = deltay;
num = deltay / 2;
numadd = deltax;
numpixels = deltay;
}
for (int curpixel = 0; curpixel <= numpixels; curpixel++)
{
// Draw the current pixel
SetPixelColor(x,y,color,bSetAlpha);
num += numadd;
if (num >= den)
{
num -= den;
x += xinc1;
y += yinc1;
}
x += xinc2;
y += yinc2;
}
}
我知道是用的Bresenham画的 但是好像是用画点画上去了 这样就2个问题 一个就是画出来的锯齿很厉害 还一个我要是想画虚线就没办法了 也没办法设置线的粗细下面是画点的
SetPixelColor(long x,long y,RGBQUAD c, bool bSetAlpha)
{
if ((pDib==NULL)||(x<0)||(y<0)||
(x>=head.biWidth)||(y>=head.biHeight)) return;
if (head.biClrUsed)
BlindSetPixelIndex(x,y,GetNearestIndex(c));
else {
BYTE* iDst = info.pImage + y*info.dwEffWidth + x*3;
*iDst++ = c.rgbBlue;
*iDst++ = c.rgbGreen;
*iDst = c.rgbRed;
}void BlindSetPixelIndex(long x,long y,BYTE i)
{
#ifdef _DEBUG
if ((pDib==NULL)||(head.biClrUsed==0)||
(x<0)||(y<0)||(x>=head.biWidth)||(y>=head.biHeight))
#if CXIMAGE_SUPPORT_EXCEPTION_HANDLING
throw 0;
#else
return;
#endif
#endif if (head.biBitCount==8){
info.pImage[y*info.dwEffWidth + x]=i;
return;
} else {
BYTE pos;
BYTE* iDst= info.pImage + y*info.dwEffWidth + (x*head.biBitCount >> 3);
if (head.biBitCount==4){
pos = (BYTE)(4*(1-x%2));
*iDst &= ~(0x0F<<pos);
*iDst |= ((i & 0x0F)<<pos);
return;
} else if (head.biBitCount==1){
pos = (BYTE)(7-x%8);
*iDst &= ~(0x01<<pos);
*iDst |= ((i & 0x01)<<pos);
return;
}
}
}求高人指点啊 小弟谢谢了
这里有个画虚线的类
这种往位图里画线还可以将位图设置成一个内存兼容的DC,就像画在窗口上一样。用CDC画容易多了。
从底层开始画想将线条变化就特别费劲。