情人节在线求助!为什么在ImageBox用DrawLine画不出直线 GDI+ C# IMAGEBOX 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像是picturebox控件...那么为什么线画不出来了呢~ 我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~ 我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~发代码吧=。= 我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~发代码吧=。=太长了 我分段发吧帮忙看下,琢磨一下午了namespace ImageDemo{ public partial class ImageBox : Control { public ImageBox() { InitializeComponent(); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true); _rotatePoint = Point.Empty; } private Point _imageOffset; private Image _image = null; private float _zoom = 100; private byte _alpha = 255; private float _rotationAngle = 0; private bool _ShowRotatePoint = true; private SizeMode _sizeMode = SizeMode.Normal; private Point _rotatePoint = Point.Empty; private Size _rotatePointSize = Size.Empty; private Rectangle _rotatePointRect = Rectangle.Empty; private FlipModeStyle _flipModeStyle = FlipModeStyle.Normal; private float FlipXValue = 1F; private float FlipYValue = 1F; private int FlipXOffsetSize = 0; private int FlipYOffsetSize = 0; public Image Image { get { return _image; } set { _image = value; this.Invalidate(); } } public Point ImageOffset { get { return _imageOffset; } set { _imageOffset = value; this.Invalidate(); } } public float RotationAngle { get { return _rotationAngle; } set { _rotationAngle = value; this.Invalidate(); } } public byte Alpha { get { return _alpha; } set { _alpha = value; this.Invalidate(); } } public float Zoom { get { return _zoom; } set { _zoom = value; this.Invalidate(); } } public SizeMode SizeMode { get { return _sizeMode; } set { _sizeMode = value; this.Invalidate(); } } public bool ShowRotatePoint { get { return _ShowRotatePoint; } set { _ShowRotatePoint = value; Invalidate(); } } public Point RotatePoint { get { if (_rotatePoint == Point.Empty) { _rotatePoint = new Point(this.Width / 2, this.Height / 2); } return _rotatePoint; } set { _rotatePoint = value; _rotatePointRect = Rectangle.Empty; Invalidate(); } } 我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~发代码吧=。=太长了 我分段发吧帮忙看下,琢磨一下午了namespace ImageDemo{ public partial class ImageBox : Control { public ImageBox() { InitializeComponent(); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true); _rotatePoint = Point.Empty; } private Point _imageOffset; private Image _image = null; private float _zoom = 100; private byte _alpha = 255; private float _rotationAngle = 0; private bool _ShowRotatePoint = true; private SizeMode _sizeMode = SizeMode.Normal; private Point _rotatePoint = Point.Empty; private Size _rotatePointSize = Size.Empty; private Rectangle _rotatePointRect = Rectangle.Empty; private FlipModeStyle _flipModeStyle = FlipModeStyle.Normal; private float FlipXValue = 1F; private float FlipYValue = 1F; private int FlipXOffsetSize = 0; private int FlipYOffsetSize = 0; public Image Image { get { return _image; } set { _image = value; this.Invalidate(); } } public Point ImageOffset { get { return _imageOffset; } set { _imageOffset = value; this.Invalidate(); } } public float RotationAngle { get { return _rotationAngle; } set { _rotationAngle = value; this.Invalidate(); } } public byte Alpha { get { return _alpha; } set { _alpha = value; this.Invalidate(); } } public float Zoom { get { return _zoom; } set { _zoom = value; this.Invalidate(); } } public SizeMode SizeMode { get { return _sizeMode; } set { _sizeMode = value; this.Invalidate(); } } public bool ShowRotatePoint { get { return _ShowRotatePoint; } set { _ShowRotatePoint = value; Invalidate(); } } public Point RotatePoint { get { if (_rotatePoint == Point.Empty) { _rotatePoint = new Point(this.Width / 2, this.Height / 2); } return _rotatePoint; } set { _rotatePoint = value; _rotatePointRect = Rectangle.Empty; Invalidate(); } } public Size RotatePointSize { get { if (_rotatePointSize == Size.Empty) { _rotatePointSize = new Size(6, 6); } return _rotatePointSize; } set { _rotatePointSize = value; _rotatePointRect = Rectangle.Empty; Invalidate(); } } public FlipModeStyle FlipModeStyle { get { return _flipModeStyle; } set { _flipModeStyle = value; switch (value) { case FlipModeStyle.FlipX: FlipXValue = -1F; FlipYValue = 1F; FlipXOffsetSize = ImageRectangle.Width; FlipYOffsetSize = 0; break; case FlipModeStyle.FlipY: FlipXValue = 1F; FlipYValue = -1F; FlipXOffsetSize = 0; FlipYOffsetSize = ImageRectangle.Height; break; case FlipModeStyle.FlipXY: FlipXValue = -1F; FlipYValue = -1F; FlipXOffsetSize = ImageRectangle.Width; FlipYOffsetSize = ImageRectangle.Height; break; case FlipModeStyle.Normal: default: FlipXValue = 1F; FlipYValue = 1F; FlipXOffsetSize = 0; FlipYOffsetSize = 0; break; } Invalidate(); } } private Size BaseSize { get { if (_image == null) return Size.Empty; switch (SizeMode) { case SizeMode.Normal: return _image.Size; break; case SizeMode.StretchImage: return new Size(this.Width, this.Height); break; case SizeMode.Zoom: int drawnImageWidth = this.Width; int drawnImageHeight = drawnImageWidth * _image.Height / _image.Width; if (drawnImageHeight > this.Height) { drawnImageHeight = this.Height; drawnImageWidth = drawnImageHeight * _image.Width / _image.Height; } return new Size(drawnImageWidth, drawnImageHeight); break; default: return Size.Empty; break; } } } private Size ZoomSize { get { if (_image == null || _zoom <= 0) return Size.Empty; Size baseSize = BaseSize; return new Size((int)(baseSize.Width * ScaleSize), (int)(baseSize.Height * ScaleSize)); } } private Rectangle ImageRectangle { get { Size zoomSize = ZoomSize; if (zoomSize.IsEmpty) return Rectangle.Empty; return new Rectangle(this.Width / 2 - zoomSize.Width / 2, this.Height / 2 - zoomSize.Height / 2, zoomSize.Width, zoomSize.Height); } } private Point ImageLocation { get { return new Point(this.Width / 2 - ZoomSize.Width / 2, this.Height / 2 - ZoomSize.Height / 2); } } private Bitmap CurrentImage { get { Bitmap result = GetScaledImage(_image); ApplyAlphaToImage(result); return result; } } private float ScaleSize { get { return _zoom / 100; } } private Rectangle RotatePointRect { get { if (_rotatePointRect == Rectangle.Empty) { _rotatePointRect = new Rectangle(RotatePoint.X - RotatePointSize.Width / 2, RotatePoint.Y - RotatePointSize.Width / 2, RotatePointSize.Width, RotatePointSize.Height); } return _rotatePointRect; } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawImage(e.Graphics); } 把绘制代码放到Paint事件的处理方法中,Graphics就使用事件参数中提供的。 Demo中不会消失是因为重载了OnPaint方法。 protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); Invalidate(); } private void DrawImage(Graphics g) { if (_image != null && _zoom > 0f) { g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; Image image = CurrentImage; if (image != null) { int imageX = ImageRectangle.Left + _imageOffset.X; int imageY = ImageRectangle.Top + _imageOffset.Y; using (Matrix m = new Matrix(FlipXValue, 0F, 0F, FlipYValue, 0, 0)) { m.Translate(_imageOffset.X, _imageOffset.Y, MatrixOrder.Append); m.Translate(FlipXOffsetSize, FlipYOffsetSize, MatrixOrder.Append); m.RotateAt(_rotationAngle, RotatePoint, MatrixOrder.Append); g.Transform = m; g.DrawImage(image, ImageRectangle); } g.Transform = new Matrix(); } if (ShowRotatePoint) { using (Pen p = new Pen(Color.White, 2)) { g.DrawEllipse(p, RotatePointRect); } g.FillEllipse(Brushes.Yellow, RotatePointRect); g.DrawEllipse(Pens.Green, RotatePointRect); } } } private Bitmap GetScaledImage(Image image) { Size zoomSize = ZoomSize; if (zoomSize.IsEmpty) return null; Bitmap result = new Bitmap(ZoomSize.Width, ZoomSize.Height); using (Graphics graphics = Graphics.FromImage(result)) { graphics.DrawImage(image, new Rectangle(Point.Empty, ZoomSize)); } return result; } private unsafe void ApplyAlphaToImage(Bitmap image) { if (image == null || _alpha == 255) return; int imageWidth = image.Width; int imageHeight = image.Height; BitmapData data = null; try { data = image.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int scan0 = data.Scan0.ToInt32(); int stride = data.Stride; byte* colPixel; byte invertedAlpha = (byte)(255 - _alpha); byte* rowPixel = (byte*)scan0; for (int y = 0; y < imageHeight; y++) { colPixel = rowPixel + 3; for (int x = 0; x < imageWidth; x++) { if (*(colPixel) < invertedAlpha) { *(colPixel) = 0; } else { *(colPixel) = (byte)(*(colPixel) - invertedAlpha); } colPixel += 4; } rowPixel += stride; } } finally { if (data != null) image.UnlockBits(data); } } 我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~周末不在~晚上下班帮你看下吧。应该是重绘了的缘故。 我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~周末不在~晚上下班帮你看下吧。应该是重绘了的缘故。好的,多谢了~! 请教 关于DataGirdView的DataSource清空 需要一个cs结构的文件传输程序,有这方面的代码参考吗? Excel导出数据的问题 WinCe下面高速保存数据 c#中如何吧棋盘、棋子的图片显示到界面上??? 各位推荐一下用哪种短信猫好点. <b class=f16>-27℃~-17℃</b>的正则表达式是? 如何定义动态数组 C#中有专门输入IP地址的控件么 复合控件中,第一次运行时子控件 DataGrid 触发不出来 SortCommand 事件 请问怎么提取字符串 GDI+ C#实现图片的放大移动
我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~
我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~
我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~
我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~发代码吧=。=
我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~
我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~发代码吧=。=
太长了 我分段发吧帮忙看下,琢磨一下午了
namespace ImageDemo
{
public partial class ImageBox : Control
{
public ImageBox()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer, true);
_rotatePoint = Point.Empty;
} private Point _imageOffset;
private Image _image = null;
private float _zoom = 100;
private byte _alpha = 255;
private float _rotationAngle = 0;
private bool _ShowRotatePoint = true;
private SizeMode _sizeMode = SizeMode.Normal;
private Point _rotatePoint = Point.Empty;
private Size _rotatePointSize = Size.Empty;
private Rectangle _rotatePointRect = Rectangle.Empty;
private FlipModeStyle _flipModeStyle = FlipModeStyle.Normal;
private float FlipXValue = 1F;
private float FlipYValue = 1F;
private int FlipXOffsetSize = 0;
private int FlipYOffsetSize = 0; public Image Image
{
get
{
return _image;
}
set
{
_image = value;
this.Invalidate();
}
} public Point ImageOffset
{
get { return _imageOffset; }
set { _imageOffset = value; this.Invalidate(); }
} public float RotationAngle
{
get
{
return _rotationAngle;
}
set
{
_rotationAngle = value;
this.Invalidate();
}
} public byte Alpha
{
get
{
return _alpha;
}
set
{
_alpha = value;
this.Invalidate();
} } public float Zoom
{
get
{
return _zoom;
}
set
{
_zoom = value;
this.Invalidate();
}
} public SizeMode SizeMode
{
get
{
return _sizeMode;
}
set
{
_sizeMode = value;
this.Invalidate();
}
} public bool ShowRotatePoint
{
get { return _ShowRotatePoint; }
set { _ShowRotatePoint = value; Invalidate(); }
} public Point RotatePoint
{
get
{
if (_rotatePoint == Point.Empty)
{
_rotatePoint = new Point(this.Width / 2, this.Height / 2);
}
return _rotatePoint;
}
set
{
_rotatePoint = value;
_rotatePointRect = Rectangle.Empty;
Invalidate();
}
}
我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~
我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~发代码吧=。=
太长了 我分段发吧帮忙看下,琢磨一下午了
namespace ImageDemo
{
public partial class ImageBox : Control
{
public ImageBox()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer, true);
_rotatePoint = Point.Empty;
} private Point _imageOffset;
private Image _image = null;
private float _zoom = 100;
private byte _alpha = 255;
private float _rotationAngle = 0;
private bool _ShowRotatePoint = true;
private SizeMode _sizeMode = SizeMode.Normal;
private Point _rotatePoint = Point.Empty;
private Size _rotatePointSize = Size.Empty;
private Rectangle _rotatePointRect = Rectangle.Empty;
private FlipModeStyle _flipModeStyle = FlipModeStyle.Normal;
private float FlipXValue = 1F;
private float FlipYValue = 1F;
private int FlipXOffsetSize = 0;
private int FlipYOffsetSize = 0; public Image Image
{
get
{
return _image;
}
set
{
_image = value;
this.Invalidate();
}
} public Point ImageOffset
{
get { return _imageOffset; }
set { _imageOffset = value; this.Invalidate(); }
} public float RotationAngle
{
get
{
return _rotationAngle;
}
set
{
_rotationAngle = value;
this.Invalidate();
}
} public byte Alpha
{
get
{
return _alpha;
}
set
{
_alpha = value;
this.Invalidate();
} } public float Zoom
{
get
{
return _zoom;
}
set
{
_zoom = value;
this.Invalidate();
}
} public SizeMode SizeMode
{
get
{
return _sizeMode;
}
set
{
_sizeMode = value;
this.Invalidate();
}
} public bool ShowRotatePoint
{
get { return _ShowRotatePoint; }
set { _ShowRotatePoint = value; Invalidate(); }
} public Point RotatePoint
{
get
{
if (_rotatePoint == Point.Empty)
{
_rotatePoint = new Point(this.Width / 2, this.Height / 2);
}
return _rotatePoint;
}
set
{
_rotatePoint = value;
_rotatePointRect = Rectangle.Empty;
Invalidate();
}
}
public Size RotatePointSize
{
get
{
if (_rotatePointSize == Size.Empty)
{
_rotatePointSize = new Size(6, 6);
}
return _rotatePointSize;
}
set
{
_rotatePointSize = value;
_rotatePointRect = Rectangle.Empty;
Invalidate();
}
} public FlipModeStyle FlipModeStyle
{
get { return _flipModeStyle; }
set
{
_flipModeStyle = value;
switch (value)
{ case FlipModeStyle.FlipX:
FlipXValue = -1F; FlipYValue = 1F; FlipXOffsetSize = ImageRectangle.Width; FlipYOffsetSize = 0;
break;
case FlipModeStyle.FlipY:
FlipXValue = 1F; FlipYValue = -1F; FlipXOffsetSize = 0; FlipYOffsetSize = ImageRectangle.Height;
break;
case FlipModeStyle.FlipXY:
FlipXValue = -1F; FlipYValue = -1F; FlipXOffsetSize = ImageRectangle.Width; FlipYOffsetSize = ImageRectangle.Height;
break;
case FlipModeStyle.Normal: default:
FlipXValue = 1F; FlipYValue = 1F; FlipXOffsetSize = 0; FlipYOffsetSize = 0; break;
}
Invalidate();
}
} private Size BaseSize
{
get
{
if (_image == null)
return Size.Empty;
switch (SizeMode)
{
case SizeMode.Normal:
return _image.Size;
break;
case SizeMode.StretchImage:
return new Size(this.Width, this.Height);
break;
case SizeMode.Zoom:
int drawnImageWidth = this.Width;
int drawnImageHeight = drawnImageWidth * _image.Height / _image.Width;
if (drawnImageHeight > this.Height)
{
drawnImageHeight = this.Height;
drawnImageWidth = drawnImageHeight * _image.Width / _image.Height;
} return new Size(drawnImageWidth, drawnImageHeight);
break;
default:
return Size.Empty;
break;
}
}
} private Size ZoomSize
{
get
{
if (_image == null || _zoom <= 0)
return Size.Empty; Size baseSize = BaseSize;
return new Size((int)(baseSize.Width * ScaleSize), (int)(baseSize.Height * ScaleSize));
}
} private Rectangle ImageRectangle
{
get
{
Size zoomSize = ZoomSize;
if (zoomSize.IsEmpty)
return Rectangle.Empty; return new Rectangle(this.Width / 2 - zoomSize.Width / 2, this.Height / 2 - zoomSize.Height / 2, zoomSize.Width, zoomSize.Height);
}
} private Point ImageLocation
{
get
{
return new Point(this.Width / 2 - ZoomSize.Width / 2, this.Height / 2 - ZoomSize.Height / 2);
}
} private Bitmap CurrentImage
{
get
{
Bitmap result = GetScaledImage(_image);
ApplyAlphaToImage(result);
return result;
}
} private float ScaleSize
{
get
{
return _zoom / 100;
}
} private Rectangle RotatePointRect
{
get
{
if (_rotatePointRect == Rectangle.Empty)
{
_rotatePointRect = new Rectangle(RotatePoint.X - RotatePointSize.Width / 2, RotatePoint.Y - RotatePointSize.Width / 2, RotatePointSize.Width, RotatePointSize.Height);
}
return _rotatePointRect;
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawImage(e.Graphics);
}
{
base.OnSizeChanged(e);
Invalidate();
} private void DrawImage(Graphics g)
{
if (_image != null && _zoom > 0f)
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Image image = CurrentImage; if (image != null)
{
int imageX = ImageRectangle.Left + _imageOffset.X;
int imageY = ImageRectangle.Top + _imageOffset.Y; using (Matrix m = new Matrix(FlipXValue, 0F, 0F, FlipYValue, 0, 0))
{ m.Translate(_imageOffset.X, _imageOffset.Y, MatrixOrder.Append); m.Translate(FlipXOffsetSize, FlipYOffsetSize, MatrixOrder.Append); m.RotateAt(_rotationAngle, RotatePoint, MatrixOrder.Append); g.Transform = m; g.DrawImage(image, ImageRectangle); }
g.Transform = new Matrix();
} if (ShowRotatePoint)
{
using (Pen p = new Pen(Color.White, 2))
{
g.DrawEllipse(p, RotatePointRect); }
g.FillEllipse(Brushes.Yellow, RotatePointRect);
g.DrawEllipse(Pens.Green, RotatePointRect); }
}
} private Bitmap GetScaledImage(Image image)
{
Size zoomSize = ZoomSize;
if (zoomSize.IsEmpty)
return null; Bitmap result = new Bitmap(ZoomSize.Width, ZoomSize.Height);
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.DrawImage(image, new Rectangle(Point.Empty, ZoomSize));
}
return result;
} private unsafe void ApplyAlphaToImage(Bitmap image)
{
if (image == null || _alpha == 255)
return; int imageWidth = image.Width;
int imageHeight = image.Height; BitmapData data = null;
try
{
data = image.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int scan0 = data.Scan0.ToInt32();
int stride = data.Stride; byte* colPixel; byte invertedAlpha = (byte)(255 - _alpha); byte* rowPixel = (byte*)scan0;
for (int y = 0; y < imageHeight; y++)
{
colPixel = rowPixel + 3;
for (int x = 0; x < imageWidth; x++)
{
if (*(colPixel) < invertedAlpha)
{
*(colPixel) = 0;
}
else
{
*(colPixel) = (byte)(*(colPixel) - invertedAlpha);
}
colPixel += 4;
}
rowPixel += stride;
} }
finally
{
if (data != null)
image.UnlockBits(data);
}
}
我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~
我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~
周末不在~晚上下班帮你看下吧。应该是重绘了的缘故。
我刚去看了下,看不出来哪里刷新了啊,需要贴源码吗?一闪就消失,应该是重绘了吧~我晚上回去看看。这个画图我也做过的~
我在我的demo中是不会消失的,但是现在的实例中,我代码有点看不懂,不知道哪里重绘了~帮帮小弟吧~
周末不在~晚上下班帮你看下吧。应该是重绘了的缘故。
好的,多谢了~!