Help with image resizing

Started by
1 comment, last by HarvP 15 years, 8 months ago
I'm making a program that captures a portion of the screen and then outputs to a second smaller screen. The smaller screen gets its content from the desktop being extended, so its nothing real fancy. I'm able to get the image over just fine but when I try to stretch the image to take the whole screen it gets funny. I resize the image when I draw it. However you'll see the crazy numbers in the Size in the draw method. The screen resolution is 800X600 and will always be 800X600. So It try to resize the image to 800X600 and it was tiny. So I now have the big numbers you see below. My question is why when I try to resize the image with what the size should be, 400X600 for its only half screen, does it make the image smaller. Then using the larger numbers actually makes the image grow?
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace scrennCapTest
{
    public partial class Form1 : Form
    {
        private static Bitmap bmpScreenshot;
        private static Graphics gfxScreenshot;        
       
        
        public Form1()
        {
            InitializeComponent();
            Paint += new PaintEventHandler(OnPaint);
        }

        void OnPaint(object sender, PaintEventArgs e)
        {
            if (bmpScreenshot != null)
            {
                e.Graphics.DrawImage(bmpScreenshot, new Rectangle(new Point(0,1), new Size(1430, 1100)));
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

            // Create a graphics object from the bitmap

            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            // Take the screenshot from the upper left corner to the right bottom corner

            gfxScreenshot.CopyFromScreen(-2,60,0,0, new Size(242,313));

            // Save the screenshot to the specified path that the user has chosen
            
            this.Invalidate();
            gfxScreenshot.Dispose();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            gfxScreenshot.Dispose();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
            {
                this.Close();
            }
        }

Advertisement
*bump*
I don't have the answer, but it might help people if you:
Try explaining what tools you're using. (Is that Java?)
Note the part of your code is supposed to be the trouble area. (Obvious if you read the whole thing, but you have large irrelevant parts.)

Hope someone can help. (Also: this might be advanced enough that it'd be better for one of the graphics topics. )
Sorry about the lack of info.

I'm using C#.

Here is the problem code.

if (bmpScreenshot != null)            {                e.Graphics.DrawImage(bmpScreenshot, new Rectangle(new Point(0,1), new Size(1430, 1100)));            }


When I draw the image I'm trying to resize it so the image becomes 800X600. The form its being drawn to is 800X600. However the Size which controls the size scaled to doesn't scale correctly. The numbers above are so it fills the 800X600 form. I got that by trial and error. What I want to know is how to resize better. Is there another method I should use? Is there something wrong with the sizes? Not sure why I have to use the crazy numbers. If I use Size(800, 600) it actually makes the picture smaller.


This is the code that takes the screenshot and the original size of the image being resized above.

gfxScreenshot.CopyFromScreen(-2,60,0,0, new Size(242,313));

This topic is closed to new replies.

Advertisement