elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  graficar en C#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: graficar en C#  (Leído 2,053 veces)
ronald11

Desconectado Desconectado

Mensajes: 2


Ver Perfil
graficar en C#
« en: 25 Enero 2015, 07:07 am »

Buenas amigos.
el problema que tengo es que no logro darle un retardo al graficar dos lineas lo que quiero haces es que dibuje 1 linea pese un tiempo y dibujar la segunda linea, prove con sleep pero no pasa nada, si alguien puede ayudarme, aca les paso el fragmento de codigo  

Código
  1. private void button2_Click(object sender, EventArgs e)
  2.        {
  3.  
  4.            timer1.Enabled = true;
  5.  
  6.        }
  7.  
  8.        private void timer1_Tick(object sender, EventArgs e)
  9.        {
  10.            Bitmap b;
  11.            b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  12.            pictureBox1.Image = (Image)b;
  13.            Graphics g = Graphics.FromImage(b);
  14.            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
  15.  
  16.            Pen pa = new Pen(Color.Blue, 1);
  17.            g.DrawLine(pa, 0, 290 - 0, 50, 290 - 50);
  18.            //Thread.Sleep(200);
  19.            g.DrawLine(pa, 50, 290 - 50, 50, 290 - 100);
  20.            //Thread.Sleep(500);
  21.        }

Mod: Tema Movido y modificado, usa etiquetas GeSHi cuando publiques codigo


« Última modificación: 25 Enero 2015, 07:12 am por engel lex » En línea

Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.818



Ver Perfil
Re: graficar en C#
« Respuesta #1 en: 25 Enero 2015, 15:48 pm »

Buenas

No me queda claro lo que intentas conseguir, pero:

1) Si vas a dibujar sobre un control, debes hacerlo en el evento OnPaint (subclaseando el control), de lo contrario te verás envuelto en problemas de Flickering u otro tipo de problemas.
2) Estás instanciando objetos los cuales no estás liberando sus recursos de la memoria al terminar de usarlos.
3) El método Thread.Sleep sirve para detener la ejecución del thread actual durante el periodo especificado, es decir, tal y como lo usas lo que hace es colgar tu aplicación.
4) Un objeto de tipo Bitmap no necesita ser casteado/convertido a un objeto de tipo Image, ya que un Bitmap hereda de un Image.

Como ya he mencionado, no se que intentas conseguir, y deberías pintar en el evento OnPaint, pero con esto te puedes hacer una idea más o menos, haciéndolo de forma asíncrona:

VB.NET:
Código
  1. Imports System.Threading.Tasks
  2. Imports System.Threading
  3.  
  4. Public Class Form1
  5.  
  6.    Dim drawTask As task
  7.  
  8.    Private Sub Button1_Click(sender As Object, e As EventArgs) _
  9.    Handles Button1.Click
  10.  
  11.        drawTask = Task.Factory.StartNew(Sub()
  12.                                             Me.DrawAsync()
  13.                                         End Sub)
  14.  
  15.    End Sub
  16.  
  17.    Private Sub DrawAsync()
  18.  
  19.        Dim bmp As Bitmap
  20.  
  21.        If PictureBox1.InvokeRequired Then
  22.            PictureBox1.Invoke(Sub()
  23.                                   bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
  24.                                   PictureBox1.Image = bmp
  25.                                   PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
  26.                               End Sub)
  27.  
  28.        Else
  29.            bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
  30.            PictureBox1.Image = bmp
  31.            PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
  32.  
  33.        End If
  34.  
  35.        Using g As Graphics = Graphics.FromImage(bmp)
  36.  
  37.            Using pen As New Pen(Color.Blue, width:=1)
  38.  
  39.                g.DrawLine(pen, New Point(0, 290 - 0), New Point(50, 290 - 50))
  40.                Thread.Sleep(200)
  41.  
  42.                g.DrawLine(pen, New Point(50, 90 - 50), New Point(50, 290 - 100))
  43.                Thread.Sleep(500)
  44.  
  45.            End Using
  46.  
  47.        End Using
  48.  
  49.        bmp.Dispose()
  50.  
  51.    End Sub
  52.  
  53. End Class

C#:
Código
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Threading.Tasks;
  8. using System.Threading;
  9.  
  10. public class Form1
  11. {
  12. task drawTask;
  13.  
  14. private void Button1_Click(object sender, EventArgs e)
  15. {
  16. drawTask = Task.Factory.StartNew(() => { this.DrawAsync(); });
  17. }
  18.  
  19. private void DrawAsync()
  20. {
  21. Bitmap bmp;
  22.  
  23. if (PictureBox1.InvokeRequired) {
  24. PictureBox1.Invoke(() =>
  25. {
  26. bmp = new Bitmap(PictureBox1.Width, PictureBox1.Height);
  27. PictureBox1.Image = bmp;
  28. PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
  29. });
  30. } else {
  31. bmp = new Bitmap(PictureBox1.Width, PictureBox1.Height);
  32. PictureBox1.Image = bmp;
  33. PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
  34. }
  35.  
  36. using (Graphics g = Graphics.FromImage(bmp)) {
  37.  
  38. using (Pen pen = new Pen(Color.Blue, width: 1)) {
  39.  
  40. g.DrawLine(pen, new Point(0, 290 - 0), new Point(50, 290 - 50));
  41. Thread.Sleep(200);
  42.  
  43. g.DrawLine(pen, new Point(50, 90 - 50), new Point(50, 290 - 100));
  44. Thread.Sleep(500);
  45. }
  46. }
  47. bmp.Dispose();
  48. }
  49. }
  50.  
  51. //=======================================================
  52. //Service provided by Telerik (www.telerik.com)
  53. //=======================================================
  54.  

Saludos


« Última modificación: 25 Enero 2015, 16:04 pm por Eleкtro » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
graficar en php usando jpgraph
PHP
pet_cx 2 4,445 Último mensaje 5 Noviembre 2008, 20:00 pm
por pet_cx
graficar estadistica con jpgraph
PHP
blonder413 1 2,650 Último mensaje 8 Octubre 2010, 00:23 am
por Shell Root
Graficar...?
Java
soser 3 2,940 Último mensaje 21 Noviembre 2010, 03:27 am
por soser
Graficar un árbol XML
Programación General
dooque 5 3,285 Último mensaje 3 Abril 2013, 00:43 am
por Pablo Videla
como graficar en cst
Materiales y equipos
leopo 0 1,873 Último mensaje 6 Noviembre 2014, 17:30 pm
por leopo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines