Dynamic load Images to Picturebox error

0
0

Hi all,

I have problem when dynamic loading image from SQL Server and display to picture box. Images be continuously saved to SQL .

If i using only one client session, there no problem. But when I open 2 or more client sessions and trying command to loading image (only one image about 300KB), it be hang and make to remain sessions can’t loading image. Until I closing 2nd client which make hang, then remain  sessions automatic re-load image again by last command. If I doing new manual command to loading image, even client 1, it will be hang again!

I had trying:

  1. I’m using global image variable type to store image for all client session, but it is same too.
  2. I’m using global array images to store image, each session has one item, it same too.
  3. It same when I load image from local folder in stead of SQL image.
  4. I have trying http image source from http url, it same too.
  5. I’m using Application.Exit() command to manual reset client session, but there is no change. I have to close this web tab.
  6. I’m using HtmlPanel background image instead of Picturebox, it is same.

Note:

  1. I’m development and publish to IIS server (latest version 10.0.17763.1) on Windows 10 64bits home edition. I’m using Visual studio 2019 and VBnet platform.
  2. If I using only one client session, it working fine.
  3. The problem only belong to image, with text I had loading from SQL, it work fine too (one or more client sessions).

Any one have solution to solver this problem, please help me!

Thanks!

  • You must to post comments
0
0

Dear Luca,

I have just fix my problem by using Wisej.HostService.exe in other discuss of your. Wisej.HostService.exe really is good solution for publishing my web site without using IIS server.

The problem may be is combine between IIS and Wisej web site built.

After using Wisej.HostService.exe, I don’t have problem related to “from file” or “stream”!

Thank you so much for  supporting!

  • You must to post comments
0
0

You need to attach a test case or I won’t be able to tell you what’s wrong in the code. I cannot reproduce the issue you describe  (IIS or IIS Express) since it’s related to the wrong usage of Image.FromFile or FromStream.

  • You must to post comments
0
0

Dear Luca,

I have just find that if I using IIS express to debug then everything fine.

But when I publishing to IIS server that have problem that have only 2 client session can be working, even my web site is nothing (no load any picture). If I open more tabs, example 10 tabs, then the tab from 3 to 10 to be hang. Until I close tab 2nd then all tabs which be hang will auto loading again!

I have trying to push other web site (not build by Wisej) to IIS server on my laptop, that working fine with more clients (IIS server version 10.0.17763.1 on Windows 10 64bits home edition)

May be have problem about connection between IIS server and web server was. Do you have any experience about publishing to IIS?

Thanks!

Attachment
  • You must to post comments
0
0

The correct way to use Image.FromFile or FromStream with System.Drawing is like this:

 using (var image = Image.FromFile(file))
 {
    return new Bitmap(image);
 }

The using keyword will dispose the image when the block is exited. It’s similar to try/finally.

The new Bitmap() line causes the full reading/decoding of the file or the stream.

If you want to use the file directly simply assign it to pictureBox1.ImageSource.

If you still have problems using the  images please attach a test case showing the issue and the code you are using.

 

  • You must to post comments
0
0

Dear Luca,

You just sent me good information!

I understand of your attention.

I’ve creating the test case as below: 

  • As your notification, if I don’t release stream, the file will being lock. Yes, I can test by change the file name after command loading image. The system will say to me that “The action can’t be completed because the file is open in IIS  worker process” as image attachment.

stream = New FileStream(“c:\DM_DC_MB.jpg”, FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(stream)

  •  If I immediately release after set PictureBox1.Image, then the image can’t display on PictureBox1:

stream = New FileStream(“c:\DM_DC_MB.jpg”, FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(stream)
stream.Close() // or  stream.Dispose

  • If I delay release after set PictureBox1.Image, when it completed load and display image (when I seen picture), the file will being release:

stream = New FileStream(“c:\DM_DC_MB.jpg”, FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(stream)

In another button, I’m using: stream.Close(). After that I can change DM_DC_MB.jpg filename => successful to unlock file.

  • But, even the file completed unlock, It still have a problem: 3rd chrome tab to be hang after 1st & 2nd tab successful loading image… => Lock file doesn’t seem to be an error

I have one question that how to use PictureBox1.ImageSource with local file? I have testing some local path file but it can’t load image.

 

Thank you so much!

Attachment
  • Tuyen NL
    Moreover, If I load image to resource, it still in error for >=3 client sessions: PictureBox1.Image = My.Resources.DM_DC_MB
  • You must to post comments
0
0

System.Drawing.Image.FromFile locks the file and keeps it locked. It’s not a Wisej issue, it’s the way System.Drawing works.

There are several ways to load an image from a file e release the lock:

https://stackoverflow.com/questions/6576341/open-image-from-file-then-release-lock

Additionally with Wisej you don’t need to use Image.FromFile since you can assign the file to ImageSource.

If you use Image.FromStream you will get the same problem since the stream is kept open. That’s the way it works. You have to load the image and release the file or the stream.

  • You must to post comments
0
0

Dear Luca,

I create a simple project:

  • Imports System.Drawing
  • Add one Picturebox1 and 2 Buttons: btLoad with code PictureBox1.Image = Image.FromFile(“C:\web4\WisejWebPageApplication4\DM_DC_MB.jpg”) and btClear with code PictureBox1.Image = Nothing.

I’m testing:

  • Publish to IIS that localhost:99
  • Open chrome 1st tab: Load image ok!
  • Open chrome 2nd tab: Load image ok!
  • Open chrome 3rd tab: Load image error!
  • Close 2nd tab: => 3rd tab automatic load image!
  • Open 4th tab: Load image error!
  • Close 3rd tab: > 4th tab automatic load image!

Thanks!

  • You must to post comments
0
0

Looks like your code is locking when using SQL Server. Send a small test case without using SQL server showing the issue please. I tried with a file, with a theme image, with a URL and it always works well.

  • You must to post comments
Showing 8 results
Your Answer

Please first to submit.