Click here to Skip to main content
15,910,411 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How to zoom image from webcam in C# use VS 2008?

[EDIT Chill60 - OP code from solution]

This is my code. I use webcam USB
C#
using 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 Dynamsoft.DotNet.TWAIN.Enums;
using Dynamsoft.DotNet.TWAIN.WebCamera;
using System.IO;
using System.IO.Ports;
using System.Xml;

namespace trial1
{
    public partial class Form1 : Form
    {
        SerialPort P = new SerialPort();
        string InputData = String.Empty;
        delegate void SetTextCallback(string text);
        public Form1()
        {
            InitializeComponent();
            string[] port = SerialPort.GetPortNames();
            cbCom.Items.AddRange(port);
            dynamicDotNetTwain1.IfShowUI = true;
            dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_WEBCAM;
            dynamicDotNetTwain1.IfThrowException = true;
            P.ReadTimeout = 1000;
            P.DataReceived += new SerialDataReceivedEventHandler(DataReceive);
            dynamicDotNetTwain2.IfShowUI = true;
            dynamicDotNetTwain2.SupportedDeviceType = EnumSupportedDeviceType.SDT_WEBCAM;
            dynamicDotNetTwain2.IfThrowException = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void DataReceive(object obj, SerialDataReceivedEventArgs e)
        {
            InputData = P.ReadExisting();
            if (InputData != String.Empty)
            {
                SetText(InputData);
            }
        }
        private void SetText(string text)
        {
            if (this.txtNhan.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else this.txtNhan.Text += text;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                dynamicDotNetTwain1.SelectSource();
                dynamicDotNetTwain1.SetVideoContainer(pictureBox1);
                dynamicDotNetTwain1.OpenSource();
                txtSource.Text = dynamicDotNetTwain1.CurrentSourceName;
                int count = dynamicDotNetTwain1.ResolutionForCamList.Count;
                for (int j = 0; j < count; j++)
                {
                    string tempHeight = dynamicDotNetTwain1.ResolutionForCamList[j].Height.ToString();
                    string tempWidth = dynamicDotNetTwain1.ResolutionForCamList[j].Width.ToString();
                    string tempResolution = tempWidth + "X" + tempHeight;
                    comboBox1.Items.Insert(j, tempResolution);
                    comboBox1.SelectedIndex = 0;
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            dynamicDotNetTwain1.ResolutionForCam = dynamicDotNetTwain1.ResolutionForCamList[comboBox1.SelectedIndex];
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (btnConnect.Text == "Connect")
            {
                P.Open();
                MessageBox.Show("Connected!", "Congratulation!");
                btnConnect.Text = "Disconnect";
            }
            else if (btnConnect.Text == "Disconnect")
            {
                btnConnect.Text = "Connect";
                P.Close();
            }
        }

        private void txtNhan_TextChanged(object sender, EventArgs e)
        {
            if (txtNhan.Text == "115")
            {
                try
                {
                    dynamicDotNetTwain1.EnableSource();
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
                txtNhan.Text = "";
            }
            else if (txtNhan.Text == "116")
            {
                try
                {
                    dynamicDotNetTwain2.EnableSource();
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
                txtNhan.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                dynamicDotNetTwain2.SelectSource();
                dynamicDotNetTwain2.SetVideoContainer(pictureBox2);
                dynamicDotNetTwain2.OpenSource();
                txtSource.Text = dynamicDotNetTwain2.CurrentSourceName;
                int counter = dynamicDotNetTwain2.ResolutionForCamList.Count;
                for (int i = 0; i <counter;>                {
                    string tempHeight = dynamicDotNetTwain2.ResolutionForCamList[i].Height.ToString();
                    string tempWidth = dynamicDotNetTwain2.ResolutionForCamList[i].Width.ToString();
                    string tempResolution = tempWidth + "X" + tempHeight;
                    comboBox2.Items.Insert(i, tempResolution);
                    comboBox2.SelectedIndex = 0;
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void cbCom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (P.IsOpen)
            {
                P.Close();
            }
            P.PortName = cbCom.SelectedItem.ToString();
        }
    }
}
Posted
Updated 8-May-14 2:00am
v2
Comments
ZurdoDev 8-May-14 7:52am    
Have you searched for examples and where are you stuck?
Member 10452637 8-May-14 7:57am    
yes. I imported image from webcam to my application and I searched in msdn.com. but that example use sofware diffirent my software
CHill60 8-May-14 8:01am    
By putting your code into a solution your question has dropped out of the list of unanswered questions. So that more people will see your post, I have used the "Improve question" link to move your code into your question, and will delete your solution.
Member 10452637 8-May-14 8:05am    
Yes. Thank you! I will draw experience for next time

1 solution

XML
Seems like you are using the software Dynamic .NET TWAIN. If you are also using its built-in viewer, the zoom feature is also built-in.

Here is how you use it;

<pre lang="c#">
dynamicDotNetTwain.SetViewMode(-1,-1);
dynamicDotNetTwain.Zoom = 2;
</pre>
 
Share this answer
 
Comments
Member 10452637 23-May-14 13:12pm    
but I must add your code on where in my code???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900