我有这样的问题。我正在做一个小的照片编辑器,在那里你可以选择图像,改变图像的亮度对比度,在图像上绘制文字。这是表单。
。我有这个方法。
private void DrawText(string text, int x, int y)
{
RectangleF rectf = new RectangleF(x, y, 90, 50);
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString(text.ToString(), new Font("Tahoma", Convert.ToInt32(this.textBoxSize.Text.ToString())), Brushes.Black, rectf);
g.Flush();
}
private void SetBrightness(int brightness)
{
if (!object.ReferenceEquals(this.pictureBox1.Image, null))
{
Bitmap temp = (Bitmap)this.pictureBox1.Image;
Bitmap bmap = (Bitmap)temp.Clone();
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + brightness;
int cG = c.G + brightness;
int cB = c.B + brightness;
if (cR < 0) cR = 1;
if (cR > 255) cR = 255;
if (cG < 0) cG = 1;
if (cG > 255) cG = 255;
if (cB < 0) cB = 1;
if (cB > 255) cB = 255;
bmap.SetPixel(i, j,
Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
}
}
this.pictureBox1.Image = (Bitmap)bmap.Clone();
}
}
public void SetContrast(double contrast)
{
if (!object.ReferenceEquals(this.pictureBox1.Image, null))
{
Bitmap temp = (Bitmap)this.pictureBox1.Image;
Bitmap bmap = (Bitmap)temp.Clone();
if (contrast < -100) contrast = -100;
if (contrast > 100) contrast = 100;
contrast = (100.0 + contrast) / 100.0;
contrast *= contrast;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
double pR = c.R / 255.0;
pR -= 0.5;
pR *= contrast;
pR += 0.5;
pR *= 255;
if (pR < 0) pR = 0;
if (pR > 255) pR = 255;
double pG = c.G / 255.0;
pG -= 0.5;
pG *= contrast;
pG += 0.5;
pG *= 255;
if (pG < 0) pG = 0;
if (pG > 255) pG = 255;
double pB = c.B / 255.0;
pB -= 0.5;
pB *= contrast;
pB += 0.5;
pB *= 255;
if (pB < 0) pB = 0;
if (pB > 255) pB = 255;
bmap.SetPixel(i, j,
Color.FromArgb((byte)pR, (byte)pG, (byte)pB));
}
}
this.pictureBox1.Image = (Bitmap)bmap.Clone();
}
}
在我的按钮点击我只是调用这个方法来做的工作。什么是问题,当我画的文字第二次,它不是删除和重新绘制文字,但我需要的文字是在不同的位置,而不是在旧的文字每次绘制。你能建议任何解决方案吗?谢谢
1条答案
按热度按时间dphi5xsq1#
有两种解决方案:
1.不要在位图上绘制;而是在它上面透明位图上绘制。
1.保留原始位图,并在绘制之前复制它。
一旦更改了位图的像素,就无法撤消,除非保留原始数据。