graficar en C#

Iniciado por ronald11, 25 Enero 2015, 07:07 AM

0 Miembros y 1 Visitante están viendo este tema.

ronald11

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 (csharp) [Seleccionar]
private void button2_Click(object sender, EventArgs e)
       {

           timer1.Enabled = true;

       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           Bitmap b;
           b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
           pictureBox1.Image = (Image)b;
           Graphics g = Graphics.FromImage(b);
           pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

           Pen pa = new Pen(Color.Blue, 1);
           g.DrawLine(pa, 0, 290 - 0, 50, 290 - 50);
           //Thread.Sleep(200);
           g.DrawLine(pa, 50, 290 - 50, 50, 290 - 100);
           //Thread.Sleep(500);
       }


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

Eleкtro

#1
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 (vbnet) [Seleccionar]
Imports System.Threading.Tasks
Imports System.Threading

Public Class Form1

   Dim drawTask As task

   Private Sub Button1_Click(sender As Object, e As EventArgs) _
   Handles Button1.Click

       drawTask = Task.Factory.StartNew(Sub()
                                            Me.DrawAsync()
                                        End Sub)

   End Sub

   Private Sub DrawAsync()

       Dim bmp As Bitmap

       If PictureBox1.InvokeRequired Then
           PictureBox1.Invoke(Sub()
                                  bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
                                  PictureBox1.Image = bmp
                                  PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
                              End Sub)

       Else
           bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
           PictureBox1.Image = bmp
           PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize

       End If

       Using g As Graphics = Graphics.FromImage(bmp)

           Using pen As New Pen(Color.Blue, width:=1)

               g.DrawLine(pen, New Point(0, 290 - 0), New Point(50, 290 - 50))
               Thread.Sleep(200)

               g.DrawLine(pen, New Point(50, 90 - 50), New Point(50, 290 - 100))
               Thread.Sleep(500)

           End Using

       End Using

       bmp.Dispose()

   End Sub

End Class


C#:
Código (csharp) [Seleccionar]

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;

public class Form1
{
task drawTask;

private void Button1_Click(object sender, EventArgs e)
{
drawTask = Task.Factory.StartNew(() => { this.DrawAsync(); });
}

private void DrawAsync()
{
Bitmap bmp;

if (PictureBox1.InvokeRequired) {
PictureBox1.Invoke(() =>
{
bmp = new Bitmap(PictureBox1.Width, PictureBox1.Height);
PictureBox1.Image = bmp;
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
});
} else {
bmp = new Bitmap(PictureBox1.Width, PictureBox1.Height);
PictureBox1.Image = bmp;
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}

using (Graphics g = Graphics.FromImage(bmp)) {

using (Pen pen = new Pen(Color.Blue, width: 1)) {

g.DrawLine(pen, new Point(0, 290 - 0), new Point(50, 290 - 50));
Thread.Sleep(200);

g.DrawLine(pen, new Point(50, 90 - 50), new Point(50, 290 - 100));
Thread.Sleep(500);
}
}
bmp.Dispose();
}
}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================


Saludos