Gerax Deon Gerax Deon - 1 month ago 21
C# Question

C# Zoom In and Zoom Out On A Image

I want to make a program have zoom in and zoom out function.(2x,4x,8x)But I can't use a available zoom function.I have to write a new one.I have done importing bitmap image.And I can get rgb color for each pixel.I created matrisses colorR,colorG and colorB for r,g and b colors.After that I thought I can create the 2x zoomed image with SolidBrush on a panel.I will draw 2x image like this:

Original Image (For example 3x3 pixels) (p=pixel color and "_" for space)


p1_p2_p3

p4_p5_p6

p7_p8_p9


2x Zoomed Image (6x6 pixels because of orginal image size) (p=pixel color of orginal image and "_" for space)


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


I wrote one loop but it didn't work because it is complety wrong.So how can I write for loops ?

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) {

}

Answer

This is insane, but if you really must do it this way, then try something like this:

            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;
Comments