.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Bitmap.Clone() thread safety

Posted By:      Posted Date: April 14, 2011    Points: 0   Category :.NET Framework
Is it safe to use the output of Bitmap.Clone() on a separate thread?  Or do I need to force a deep copy (make a new bitmap and blit over the contents) to make sure it works right?

View Complete Post

More Related Resource Links

ThrottledParallelForEach thread safety

I am looking into using ThrottledParallelForEach from http://msdn.microsoft.com/en-us/library/ee620808.aspx but a bit concerned that there is no synchronization in implementation. Is it correct?

Bitmap clone do not copy the data source


Bitmap clone does not make a copy of the data source of the original bitmap??

as you can see, if I change the value of imData, bmp2 will change too! So if I want to make a REAL copy of a bitmap I need to create a new bitmap, marshall copy the scan0 from the original to the new one?

    public void BmpTest()
      Size s = new Size(80, 60);
      int imgSize = s.Width * s.Height;
      byte[] imgData;
      imgData = new byte[imgSize];
      GCHandle gch = GCHandle.Alloc(imgData, GCHandleType.Pinned);
      IntPtr dataPtr = gch.AddrOfPinnedObject();
      Bitmap bmp;
      bmp = new Bitmap(s.Width, s.Height, s.Width, PixelFormat.Format8bppIndexed, dataPtr);
      Bitmap bmp2;
      bmp2 = (Bitmap)bmp.Clone();
      this.pcbImage.Image = bmp2;
      for (int i = 0; i < 200; i++)
        imgData[i] = 255;

CryptographicException: The supplied handle is invalid. Possible thread safety issue?


I was seeing a "The supplied handle is invalid" message in CryptographicExceptions intermittently, so I created the following program to reproduce the problem based on the hunch that there was a concurrency issue. Originally the problem was occurring when encrypting data, but I've pruned away as much code as I can to make it smaller/easier to understand (of course the KeyWork method doesn't do too much). 

Note: this is CPU intensive and takes a while to complete. Sorry.

The code:


using System;
using System.Security.Cryptography;
using System.Text;

public class P
	public static void Main()
		byte[] public_encrypting_key = StringToByteArray("45434B3542000000016FE13A42A9D54503BC694E9E298760FC03BA99FB224691BF03830BA9FB58354227541F120667843A8813628ADA70126EA06CBCB3537A1986283D3FA3612F124673010CA5F372A32B50E6B648F61DCF70B3E3782324AA6795891ACD8B87F9CA89F128BD

Bitmap FromFile space "file not found"


I am using aspose.slides to create PPTx's. Some of the process involves loading jpg's from another server and including them in the presentation. This works fine in my development environment. When I upload the site to its server, the jpg loading fails.

I am trying to access jpg's on the other server by:

   oFS = New FileStream(sFile, FileMode.Open, FileAccess.Read)

This causes an access violation error when run from the server.

I also boiled the code down to reading the file directly:

   oImg = System.Drawing.Bitmap.FromFile(sFilePath)

This causes a file not found error.

We have narrowed the problem down to the fact that the File Path has a space in it. How does one solve this problem (while still keeping the space, there is a lot of legacy data)? I have tried replacing the space with %20 or +. I have tried using Server.UrlEncode. The error message always comes back with the encoding in place (like no decoding

Bitmap to Image Control - ImageUrl


 This is my first attempt at using the drawing features of asp.net.  I just want to start simple, and create a single color bar based on a value for a single graph.  I wrote this so far, but I can't figure out how to take the bitmap I just created, and apply it to the Image Control. Do I have to save the file first, and then reload it.

Plus, my box keeps coming out pure black, I trying to fill the box with a color. 

This is part of a web custom control, in which I dynamically created an Image Control. I want to apply the bitmap I made to the image control. 


Dim imgGoogle As Image
imgGoogle = New Image
imgGoogle.ImageAlign = ImageAlign.AbsMiddle
imgGoogle.ImageUrl = cs.GetWebResourceUrl(rsType, "ice5software.BotCatcher.Google.gif")
Private Sub DrawBarGraph_Google(ByVal BarValue As Integer)

	Dim objBitmap As System.Drawing.Bitmap
	Dim objGraphics As S

Thread Performance: Resource Contention Concurrency Profiling in Visual Studio 2010


Visual Studio 2010 includes new resource contention profiling features that help detect concurrency contention among threads. We walk through a profiling investigation to demonstrate how you can pinpoint and fix resource contention problems in your code.

Maxim Goldin

MSDN Magazine June 2010

Thread Diagnostics: Performance Tuning with The Concurrency Visualizer in Visual Studio 2010


Understanding performance bottlenecks in multithreaded apps used to require significant instrumentation and analysis by expert developers. The Concurrency Visualizer in Visual Studio 2010 significantly reduces the burden of parallel performance analysis.

Hazim Shafi

MSDN Magazine March 2010

CLR Inside Out: Thread Management In The CLR


Getting the performance you want in concurrent applications is not as straightforward as you might think. See how common threading issues can affect your application.

Erika Fuentes and Eric Eilebrecht

MSDN Magazine December 2008

Editor's Note: new Thread(ReadEditorsNote).Start(); yourAttention.WaitOne();


Introducing the issue, Howard Dierking points out that you can't simply parallelize your code blindly if you expect to truly reap the benefits that parallelism promises.

Howard Dierking

MSDN Magazine October 2008

Pooled Threads: Improve Scalability With New Thread Pool APIs


The new threadpool APIs in Windows Vista and in the upcoming Windows Server 2008 provide advances that even native Windows developers can benefit from.

Robert Saccone

MSDN Magazine October 2007

Concurrent Affairs: Build a Richer Thread Synchronization Lock


In my last column, I showed the various thread synchronization mechanisms employed by the Microsoft® . NET Framework (see Concurrent Affairs: Performance-Conscious Thread Synchronization). I then examined the performance characteristics of all these mechanisms and determined that the Interlocked methods performed the best because the calling thread never has to transition to kernel mode.

Jeffrey Richter

MSDN Magazine March 2006

Concurrent Affairs: Performance-Conscious Thread Synchronization


In my career, I have architected and implemented many thread synchronization techniques. This has provided me with a lot of experience that has shaped the way I now think about thread synchronization problems.

Jeffrey Richter

MSDN Magazine October 2005

Safety in Windows: Manage Access to Windows Objects with ACLs and the .NET Framework


Until now, Microsoft did not provide explicit support in the .NET Framework for manipulating security settings. With the .NET Framework 1.x, access can only be granted to users via a series of cumbersome P/Invoke calls. By introducing the concepts of security objects and rules, the .NET Framework 2.0 allows developers to manipulate security settings of objects in a few easy steps using managed code. Want to know more? Read on.

Mark Novak

MSDN Magazine November 2004

Basic Instincts: Thread Synchronization


My last three Basic Instincts columns have examined techniques for using asynchronous delegates and creating secondary threads. Those columns demonstrated how to introduce multithreaded behavior into your applications.

Ted Pattison

MSDN Magazine September 2004

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend