Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to draw Efficiently on Picture Box in C#

ummarbhuttaummarbhutta Posts: 4Member
I am developing a Remoting application in C#, through which I can view the remote system over the LAN, I am facing problem in case of videos.. I am not receiving the Real Time video on the remote system. its a little bit slow. with profiler I have checked that the time is being taken on two following fuction calls

[code]
private void PlaceUpdateOnBmp(Bitmap newBitmap, int x1, int y1, int x2, int y2)
{
try
{
bool resChangeFlag = false;
Rectangle rect = new Rectangle(x1, y1, x2, y2);

if (Screen.AllScreens[0].Bounds.Width != clientScreenX / noOfScreens || Screen.AllScreens[0].Bounds.Height != clientScreenY)
bmp = new Bitmap(1024, 768);
g = Graphics.FromImage(bmp);


g.DrawImage(newBitmap, rectf); //This fuction takes very much time
g.Flush();
}
catch (Exception e)
{
}

}
[/code]

and the data I received from the sever is in JPEG form in a Memorty stream and when I make the Bitmap object from that MemoryStream its takes the maximum time
[code]
//This function takes the maximum time
newBitmap = new Bitmap((MemoryStream)receivedImage);
[/code]

One thing I have noticed that If I don't assign the final bitmap to the PictureBox then these functions don't take time, however when I assign them to a PictureBox Colntrol (Mean show them to real screen).. They begin to take time.. It seems that Invalidate call is making some locks behind the scence... Is there any way to avoid these locks or whatever this delay is..???

Please help me out of it..

Regards
Ummar

Comments

  • ummarbhuttaummarbhutta Posts: 4Member
    Please I am still waiting for any suggesstion??
  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    So, wasn't sure about what you were trying to accomplish in your code - but if your just trying to get a copy of what's on a computer's screen onto a bitmap - the most efficient way I am aware to do this is like so:
    [code]
    var bitmap = new Bitmap(1024, 768);
    var graphics = Graphics.FromImage(bitmap);
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
    graphics.Dispose();
    [/code]

    Now as far as why the new Bitmap from memory stream takes a long time is most likely because it is downloading a huge chunk of data and it can't return from creating that bitmap until the whole thing is downloaded and formed.

    The microsoft RemoteDesktop application is actually using a lot of compression on the data they are sending - that is really going to be what you are going to have to do also. You're going to be doing a lot of re-inventing of wheels to complete this application.
    ><//~Psightoplasm`~
  • ummarbhuttaummarbhutta Posts: 4Member
    Thanks for your reply! actually I am not trying to capture the screen (copy the screen to bitmap), as I have done it already and it has been optimized as much as possible..(This part is on server side), but I am talking about the client side, where I want to draw the bitmap on client screen as efficiently as possible, the point you have mentioned that it takes a lot of time to decode the MemoryStream data in to Bitmap. this is true but when I don't use the Invalidate call (to refresh my screen) the time of this funtion reduce dramatically. Any suggesstion?
  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    Ya, I'm not sure using a picturebox is going to be your best bet for drawing frames. You might try creating your own control - something like this:

    [code]
    public class BitmapPresenter : Control
    {
    protected override void OnPaint(PaintEventArgs e)
    {
    //Do Nothing
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
    //Do Nothing
    }

    public void DrawBitmap(Bitmap img)
    {
    var g = CreateGraphics();
    g.DrawImage(img, 0, 0);
    g.Dispose();
    img.Dispose();
    }
    }
    [/code]

    This is one approach for GDI+ - this would cut out most of the control's drawing overhead. - but I think you are going to want to look into DirectX for the most optimized output. - DirectX is a trusted application and has much lower level access than you do.
    ><//~Psightoplasm`~
Sign In or Register to comment.