viernes, 26 de septiembre de 2014

El Demo del Día: Visor de Imágenes con Zoom en WinForms

Visor de Imágenes con Zoom en WinForms

Este post es a pedido de un alumno que quiere una aplicación en Windows Forms que muestre una secuencia de Imágenes y pueda realizar el Zoom.

Crear la Aplicación Windows Forms

Primero crearemos una aplicación en Windows Forms en C# llamada: "VisorImagenesZoom".
Cambiar de nombre al formulario por el de "frmVisor", luego diseñar la pantalla como se muestra en la siguiente figura:


Nota: También hay que incluir un Timer llamado "tmrPreview" que cada 3 segundos muestre las imágenes del directorio seleccionado.

Escribir el siguiente código en el formulario:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace VisorImagenesZoom
{
    public partial class frmVisor : Form
    {
        private int c;
        private int n;
        private Point punto1;

        public frmVisor()
        {
            InitializeComponent();
        }

        private void abrirDirectorio(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog().Equals(DialogResult.OK))
            {
                txtDirectorio.Text = fbd.SelectedPath;
                IEnumerable<string> archivos = Directory.EnumerateFiles(fbd.SelectedPath, "*.jpg");
                lstArchivo.Items.Clear();
                lstArchivo.BeginUpdate();
                foreach (string archivo in archivos)
                {
                    lstArchivo.Items.Add(Path.GetFileNameWithoutExtension(archivo));
                }
                lstArchivo.EndUpdate();
                txtTotalArchivos.Text = lstArchivo.Items.Count.ToString();
                tmrPreview.Enabled = true;
                tmrPreview.Start();
            }
        }

        private void mostrarImagenes(object sender, EventArgs e)
        {
            lstArchivo.SelectedIndex = c;
            string archivo = Path.Combine(txtDirectorio.Text, lstArchivo.Items[c].ToString() + ".jpg");
            picPreview.LoadAsync(archivo);
            c++;
            if (c == lstArchivo.Items.Count) c = 0;
        }

        private void configurarZoom(object sender, MouseEventArgs e)
        {
            if (e.Button.Equals(MouseButtons.Left))
            {
                if (n == 0)
                {
                    punto1 = e.Location;
                    tmrPreview.Stop();
                    tmrPreview.Enabled = false;
                }
                else
                {
                    if (n == 1)
                    {
                        Point punto2 = e.Location;
                        string archivo = Path.Combine(txtDirectorio.Text,
                                                   lstArchivo.Items[c].ToString() + ".jpg");
                        Bitmap bmp = new Bitmap(picPreview.Image);
                        Bitmap bmpZoom = new Bitmap(bmp.Width, bmp.Height);
                        Graphics g = Graphics.FromImage(bmpZoom);
                        float ratioX = (bmp.Width/picPreview.Width);
                        float ratioY = (bmp.Height / picPreview.Height);
                        int posX = (int)(ratioX * punto1.X);
                        int posY = (int)(ratioY * punto1.Y);
                        int ancho = (int)(ratioX*(punto2.X-punto1.X));
                        int alto = (int)(ratioY * (punto2.Y - punto1.Y));
                        Rectangle srcRect = new Rectangle(posX, posY, ancho, alto);
                        Rectangle dstRect = new Rectangle(0, 0, bmpZoom.Width, bmpZoom.Height);
                        g.DrawImage(bmp, dstRect, srcRect, GraphicsUnit.Pixel);
                        picPreview.Image = bmpZoom;
                        tmrPreview.Enabled = true;
                        tmrPreview.Start();
                    }
                }
                n++;
                if (n == 2) n = 0;
            }
        }
    }
}

Nota: Para programar el Zoom se ha asociado al evento MouseClick del PictureBox llamado "picPreview" donde en el primer clic se captura la ubicación del punto y se guarda en una variable "punto1" y en el segundo clic se captura el punto2 y se crea un rectangulo con la imagen para el Zoom. Solo hay que tener en cuenta que el Rectangulo debe ser en base a la imagen (Bitmap) del PictureBox y No al tamaño del control (con propiedad SizeMode en StrechImage), por lo cual se crea las variables ratios.

Probar la Aplicación Creada

Grabar el proyecto y ejecutarlo con F5. Seleccionar un directorio donde se encuentren archivos jpg y se mostraran en la lista tal como aparece en la siguiente figura:


Cada 3 segundos cambiara la imagen y se seleccionara en la lista el archivo que se esta mostrando, dar el primer clic sobre la imagen y luego el segundo clic mostrará el preview de la imagen, tal como sigue:


Comentario Final

En este post hemos visto como mostrar una secuencia de imágenes usando un Timer y también como realizar el Zoom de una Imagen usando el método DrawImage de la clase Graphics para dibujar un rectángulo con la imagen especificada al dar dos clics, uno para marcar el punto de inicio y otro para marcar el punto final.

Descarga:
Demo19_VisorImagenesZoom



El Libro del Día: jQuery Mobile Cookbook

El Libro del Día: 2014-09-26

Titulo: jQuery Mobile Cookbook
Autor: Chetan K Jain
Editorial: Packt
Nro Paginas: 320

Capítulos:
Chapter 1: Get Rolling
Chapter 2: Pages and Dialogs
Chapter 3: Toolbars
Chapter 4: Buttons and Content Formatting
Chapter 5: Forms
Chapter 6: List Views
Chapter 7: Configurations
Chapter 8: Events
Chapter 9: Methods and Utilities
Chapter 10: The Theme Framework
Chapter 11: HTML5 and jQuery Mobile

Descarga:
jQuery_Mobile_Cookbook

jueves, 25 de septiembre de 2014

El Libro del Día: Pro jQuery Mobile

El Libro del Día: 2014-09-25

Titulo: Pro jQuery Mobile
Autor: Brad Broulik
Editorial: Apress
Nro Paginas: 262

Capítulos:
Chapter 1: Why jQuery Mobile?
Chapter 2: Getting Started with jQuery Mobile
Chapter 3: Navigating with Headers, Toolbars, and Tab Bars
Chapter 4: Form Elements and Buttons
Chapter 5: List Views
Chapter 6: Formatting Content with Grids and CSS Gradients
Chapter 7: Creating Themable Designs
Chapter 8: jQuery Mobile API
Chapter 9: Service Integration Strategies
Chapter 10: Easy Deployment with PhoneGap

Descarga:
Pro_jQuery_Mobile

miércoles, 24 de septiembre de 2014

El Libro del Día: Pro ASP.NET Web API Security

El Libro del Día: 2014-09-24

Titulo: Pro ASP.NET Web API Security
Autor: Badrinarayanan Lakshmiraghavan
Editorial: Apress
Nro Paginas: 403

Capítulos:
Chapter 1: Welcome to ASP.NET Web API
Chapter 2: Building RESTful Services
Chapter 3: Extensibility Points
Chapter 4: HTTP Anatomy and Security
Chapter 5: Identity Management
Chapter 6: Encryption and Signing
Chapter 7: Custom STS through WIF
Chapter 8: Knowledge Factors
Chapter 9: Ownership Factors
Chapter 10: Web Tokens
Chapter 11: OAuth 2.0 Using Live Connect API
Chapter 12: OAuth 2.0 from the Ground Up
Chapter 13: OAuth 2.0 Using DotNetOpenAuth
Chapter 14: Two-Factor Authentication
Chapter 15: Security Vulnerabilities
Appendix: ASP.NET Web API Security Distilled

Descarga:
Pro_ASPNET_WebAPI_Security

martes, 23 de septiembre de 2014

El Libro del Día: Practical ASP.NET Web API

El Libro del Día: 2014-09-23

Titulo: Practical ASP.NET Web API
Autor: Badrinarayanan Lakshmiraghavan
Editorial: Apress
Nro Paginas: 329

Capítulos:
Chapter 1: Building a Basic Web API
Chapter 2: Debugging and Tracing
Chapter 3: Media-Type Formatting CLR Objects
Chapter 4: Customizing Response
Chapter 5: Binding an HTTP Request into CLR Objects
Chapter 6: Validating Requests
Chapter 7: Managing Controller Dependencies
Chapter 8: Extending the Pipeline
Chapter 9: Hosting ASP.NET Web API
Chapter 10: Securing ASP.NET Web API
Chapter 11: Consuming ASP.NET Web API
Chapter 12: Building a Performant Web API

Descarga:
Practical_ASPNET_WebAPI

lunes, 22 de septiembre de 2014

El Libro del Día: Beginning ASP.NET Security

El Libro del Día: 2014-09-22

Titulo: Beginning ASP.NET Security
Autor: Barry Dorrans
Editorial: Wrox
Nro Paginas: 440

Capítulos:
CHAPTER 1 Why Web Security Matters
PART I THE ASP.NET SECURITY BASICS
CHAPTER 2 How the Web Works
CHAPTER 3 Safely Accepting User Input
CHAPTER 4 Using Query Strings, Form Fields, Events, and Browser Information
CHAPTER 5 Controlling Information
CHAPTER 6 Keeping Secrets Secret - Hashing and Encrypton
PART II SECURING COMMON ASP.NET TASKS
CHAPTER 7 Adding Usernames and Passwords
CHAPTER 8 Securely Accessing Databases
CHAPTER 9 Using the File System
CHAPTER 10 Securing XML
PART III ADVANCED ASP.NET SCENARIOS
CHAPTER 11 Sharing Data with Windows Communication Foundation
CHAPTER 12 Securing Rich Internet Applications
CHAPTER 13 Understanding Code Access Security
CHAPTER 14 Securing Internet Information Server (IIS)
CHAPTER 15 Third-Party Authentication
CHAPTER 16 Secure Development with the ASP.NET MVC Framework

Descarga:
Beginning_ASPNET_Security

domingo, 21 de septiembre de 2014

El Libro del Día: Beginning ASP.NET 4.5 in C#

El Libro del Día: 2014-09-21

Titulo: Beginning ASP.NET 4.5 in C#
Autor: Matthew MacDonald
Editorial: Apress
Nro Paginas: 900

Capítulos:
Part 1: Introducing .NET
Chapter 1: The Big Picture
Chapter 2: The C# Language
Chapter 3: Types, Objects, and Namespaces
Part 2: Developing ASP.NET Applications
Chapter 4: Visual Studio
Chapter 5: Web Form Fundamentals
Chapter 6: Web Controls
Chapter 7: Error Handling, Logging, and Tracing
Chapter 8: State Management
Part 3: Building Better Web Forms
Chapter 9: Validation
Chapter 10: Rich Controls
Chapter 11: User Controls and Graphics
Chapter 12: Styles, Themes, and Master Pages
Chapter 13: Website Navigation
Part 4: Working with Data
Chapter 14: ADO.NET Fundamentals
Chapter 15: Data Binding
Chapter 16: The Data Controls
Chapter 17: Files and Streams
Chapter 18: XML
Part 5: Website Security
Chapter 19: Security Fundamentals
Chapter 20: Membership
Chapter 21: Profiles
Part 6: Advanced ASP.NET
Chapter 22: Component-Based Programming
Chapter 23: Caching
Chapter 24: LINQ and the Entity Framework
Chapter 25: ASP.NET AJAX
Chapter 26: Deploying ASP.NET Applications

Descarga:
Beginning_ASPNET_4.5_C#