C#放大并缩小图像

时间:2016-10-07 17:28:19

标签: c#

我想让程序具有放大和缩小功能。(2x,4x,8x)但是我不能使用可用的缩放功能。我必须写一个新的。我已经完成了导入位图图像。我可以为每个像素获得rgb颜色。我为r,g和b颜色创建了matrisses colorR,colorG和colorB。之后我想我可以在面板上使用SolidBrush创建2x缩放图像。我将绘制2x图像,如下所示:

原始图像(例如3x3像素)(p =像素颜色,空格为“_”)

  

p1_p2_p3

     

p4_p5_p6

     

p7_p8_p9

2x Zoomed Image (由于原始图像尺寸,为6x6像素)(p =原始图像的像素颜色和空间的“_”)

  

p1_p1_p2_p2_p3_p3

     

p1_p1_p2_p2_p3_p3

     

p4_p4_p5_p5_p6_p6

     

p4_p4_p5_p5_p6_p6

     

p7_p7_p8_p8_p9_p9

     

p7_p7_p8_p8_p9_p9

我写了一个循环,但它没有用,因为它是完全错误的。那么我怎样才能为 循环编写

    private void button4_Click(object sender, EventArgs e) {
        listBox1.Items.Clear();//insignificant
        listBox2.Items.Clear();//insignificant
        listBox3.Items.Clear();//insignificant

        using (OpenFileDialog dlg = new OpenFileDialog()) {
            dlg.Title = "Open Image";
            dlg.Filter = "*.bmp|*.bmp|*.*|*.*";

            if (dlg.ShowDialog() == DialogResult.OK) {
                pictureBox1.Image = new Bitmap(dlg.FileName);
            }
        }
    }

    private void button1_Click(object sender, EventArgs e) {
        Graphics my2xImage = panel1.CreateGraphics();

        int bmpHeight = pictureBox1.Image.Height;
        int bmpWidth = pictureBox1.Image.Width;
        Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();

        int[,] colorR = new int[bmpHeight, bmpWidth];
        int[,] colorG = new int[bmpHeight, bmpWidth];
        int[,] colorB = new int[bmpHeight, bmpWidth];



        for (int y = 0; y < bmpHeight; y++) {
            for (int x = 0; x < bmpWidth; x++) {
                Color pixelColor = bmpFirst.GetPixel(x, y);

                colorR[x, y] = pixelColor.R;
                colorG[x, y] = pixelColor.G;
                colorB[x, y] = pixelColor.B;

                listBox1.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorR[x, y]);//insignificant
                listBox2.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorG[x, y]);//insignificant
                listBox3.Items.Add("(" + (x + 1) + "," + (y + 1) + ")" + " " + colorB[x, y]);//insignificant

            }

        }



        //for (int y = 0; y < (bmpHeight * 2); y++)
        //{
        //    for (int x = 0; x < (bmpWidth * 2); x++)
        //    {
        //        Color mySpecialColor = Color.FromArgb(colorR[x,y], colorG[x,y], colorB[x,y]);
        //        SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
        //        my2xImage.FillRectangle(pixelBrush, x, y, 1, 1);
        //    }
        //}


    }

    private void button5_Click(object sender, EventArgs e) {

    }

    private void button2_Click(object sender, EventArgs e) {

    }

5 个答案:

答案 0 :(得分:0)

您应该使用DrawImage类的Graphics方法。

Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();

Graphics g = Graphics.FromImage(bmpFirst);

// Draw image to screen.
g.DrawImage(newImage, destRect, x, y, width, height, units);

请看这里:https://msdn.microsoft.com/en-us/library/ms142045(v=vs.110).aspx

另见:https://msdn.microsoft.com/en-us/library/k0fsyd4e(v=vs.110).aspx

您甚至可以设置插值模式:https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx

您正在寻找NearestNeighbor插值模式。

答案 1 :(得分:0)

这是疯了,但是如果你真的必须这样做,那么尝试这样的事情:

            int dx = x*2;
            int dy = y*2;

            colorR[dx  ,dy  ] = pixelColor.R;
            colorR[dx+1,dy  ] = pixelColor.R;
            colorR[dx  ,dy+1] = pixelColor.R;
            colorR[dx+1,dy+1] = pixelColor.R;

            colorG[dx  ,dy  ] = pixelColor.G;
            colorG[dx+1,dy  ] = pixelColor.G;
            colorG[dx  ,dy+1] = pixelColor.G;
            colorG[dx+1,dy+1] = pixelColor.G;

            colorB[dx  ,dy  ] = pixelColor.B;
            colorB[dx+1,dy  ] = pixelColor.B;
            colorB[dx  ,dy+1] = pixelColor.B;
            colorB[dx+1,dy+1] = pixelColor.B;

答案 2 :(得分:0)

他是解决方案。

private void button4_Click(object sender, EventArgs e       )
    {
        listBox1.Items.Clear();
        listBox2.Items.Clear();
        listBox3.Items.Clear();

        using (OpenFileDialog dlg = new OpenFileDialog())
        {
            dlg.Title = "Open Image";
            dlg.Filter = "*.bmp|*.bmp|*.*|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(dlg.FileName);
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {


        int bmpHeight = pictureBox1.Image.Height;
        int bmpWidth = pictureBox1.Image.Width;
        Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
        Graphics g = panel1.CreateGraphics();




        int[,] colorR = new int[bmpHeight*2 , bmpWidth*2];
        int[,] colorG = new int[bmpHeight*2 , bmpWidth*2];
        int[,] colorB = new int[bmpHeight*2 , bmpWidth*2];



        for (int y = 0; y < bmpHeight; y++)
        {
            for (int x = 0; x < bmpWidth; x++)
            {
                Color pixelColor = bmpFirst.GetPixel(x, y);

                int dx = x * 2;
                int dy = y * 2;

                colorR[dx, dy] = pixelColor.R;
                colorR[dx + 1, dy] = pixelColor.R;
                colorR[dx, dy + 1] = pixelColor.R;
                colorR[dx + 1, dy + 1] = pixelColor.R;

                colorG[dx, dy] = pixelColor.G;
                colorG[dx + 1, dy] = pixelColor.G;
                colorG[dx, dy + 1] = pixelColor.G;
                colorG[dx + 1, dy + 1] = pixelColor.G;

                colorB[dx, dy] = pixelColor.B;
                colorB[dx + 1, dy] = pixelColor.B;
                colorB[dx, dy + 1] = pixelColor.B;
                colorB[dx + 1, dy + 1] = pixelColor.B;




            }

        }



        for (int y = 0; y < (bmpHeight*2); y++)
        {
            for (int x = 0; x < (bmpWidth*2); x++)
            {
                Color mySpecialColor = Color.FromArgb(colorR[x, y], colorG[x, y], colorB[x, y]);
                SolidBrush pixelBrush = new SolidBrush(mySpecialColor);
                g.FillRectangle(pixelBrush, x, y, 1, 1);
            }
        }

答案 3 :(得分:0)

您可以将 for循环 用于 颜色[dx,dy] 部分。这是8x放大。

        int bmpHeight = pictureBox1.Image.Height;
        int bmpWidth = pictureBox1.Image.Width;

        Bitmap bmpFirst = (Bitmap)pictureBox1.Image.Clone();
        Graphics g = panel3.CreateGraphics();

        int[,] colorR = new int[bmpHeight * 8, bmpWidth * 8];
        int[,] colorG = new int[bmpHeight * 8, bmpWidth * 8];
        int[,] colorB = new int[bmpHeight * 8, bmpWidth * 8];

        for (int y = 0; y < bmpHeight; y++)
        {
            for (int x = 0; x < bmpWidth; x++)
            {
                Color pixelColor = bmpFirst.GetPixel(x, y);

                int dx = x * 8;
                int dy = y * 8;

                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        colorR[dx + j, dy + i] = pixelColor.R;
                        colorG[dx + j, dy + i] = pixelColor.G;
                        colorB[dx + j, dy + i] = pixelColor.B;
                    }
                }

            }

答案 4 :(得分:0)

完整代码(您也可以下载项目:Link

'options'