簡體   English   中英

C#-在位圖上繪制Winforms

[英]C# - Winforms Drawing on Bitmap

我有這種問題。 我正在制作一個小型照片編輯器,您可以在其中選擇圖像,更改圖像的亮度對比度,在圖像上繪制文本。 這是表格。 在此處輸入圖片說明 我有這種方法。

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();
        }
    }

在我的按鈕CLick上,我只是調用此方法來完成工作。 第二次繪制文本時出現了什么問題,它不是刪除並再次繪制文本,而是每次都需要在不同的位置繪制文本,並且不要覆蓋舊文本。 您能對此提出任何解決方案嗎? 謝謝

有兩種解決方案:

  1. 不要在位圖上繪制; 而是在其頂部繪制不可見的位圖。

  2. 保留原始位圖並在繪制之前復制它。

更改位圖的像素后,除非保留原始數據,否則無法撤消該操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM