MagickNET exception when resample PixelCacheAllocationFailed - c#

I use MagickNET in a ASP .NET application. Usually it's works but sometimes with somes files (.jpg, .png) I receive an exception when I resample the picture with another DPI
ImageMagick.MagickResourceLimitErrorException: iisexpress.exe: PixelCacheAllocationFailed `D:\Data\IT Tools and Applications\Code\Zola\ZolaIntranet\Upload\Temp\4ffe031b-b559-4b5c-8f88-e1a5b8e4fbe7.jpg' # error/cache.c/OpenPixelCache/3369
Here the line where the exception happens :
tmpImage.Resample(new PointD(300,300));
When I tried to use a smaller DPI (72 or less) I got another exception :
ImageMagick.MagickCacheErrorException: iisexpress.exe: UnableToExtendCache 'D:\Data\IT Tools and Applications\Code\Zola\ZolaIntranet\Upload\Temp\42458df9-f7ee-4e4b-abfd-628317b4b7eb.jpg': Invalid argument # error/cache.c/OpenPixelCache/3536 ---> ImageMagick.MagickCacheErrorException: iisexpress.exe: UnableToExtendCache
Can someone have an idea ?

For one or another reason, for few pictures in PNG format ImageMagick cant't find the density of the picture. You have to specify a density before calling Resample() function and that resolve my problem.
PointD density = new PointD(300,300);
tmpImage.Density = density;
tmpImage.Resample(density);
tmpImage.Write(image);

Related

How can I set an specific Icon on a Marker in Xamarin GoogleMaps

I would create an Android App where I can show geo data and other things.
I implement Android.Gms.Maps and would change the default ping icon of a marker into an dot.
I tried different ways but nothing worked for me...please help me!
attempt 1: (Open the bmp icon from path)
_myPositionMarker = _map.AddMarker(new MarkerOptions()
.SetPosition(new LatLng(34.8094, 12.7484))
.SetTitle("Actual Position")
.SetIcon(BitmapDescriptorFactory.FromPath("C:\\Users\\my.name\\source\\repos\\repo\\appName\\Resources\\drawable\\dot.bmp")));
error message: failed to decode image, must be a bitmap
attempt 2: (I have included the bmp file as embedded resource)
_myPositionMarker = _map.AddMarker(new MarkerOptions()
.SetPosition(new LatLng(34.8094, 12.7484))
.SetTitle("Actual Position")
.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.dot)));
error message: the app did'nt compile but no failure is shown
attempt 3: (also as resource)
var markerImg = BitmapDescriptorFactory.FromResource(Resource.Drawable.dot);
_myPositionMarker = _map.AddMarker(new MarkerOptions()
.SetPosition(new LatLng(34.8094, 12.7484))
.SetTitle("Actual Position")
.SetIcon(markerImg));
error message: the app did'nt compile but no failure is shown
nothing worked! I have no idea to solve the problem :-/
to be sure...
_myPositionMarker = _map.AddMarker(new MarkerOptions()
.SetPosition(new LatLng(24.8094, 12.7484))
.SetTitle("Actual Position")
...shows the default marker correctly and the app work well
Thanks for helping!
Attempt 1 won't work as the path does not point at a path on the device itself.
Attempt 2 and 3 should work as long as the Drawable is not a VectorDrawable or XML based drawable. It needs to be an jpg or png.
These also can't be embedded resources, but have to be Android Resources, so they need to be in the appropriate Resources folder and Build Action set to AndroidResource.
Also make sure to add markers after the map ready callback has been called.

C# System.Drawing.Image.get_Width() throws exception on WinForms form is maximized

I writing Windows Forms Application application which should show image on the PictureBox control.
To retrieve this image from DICOMDIR file I use fo-dicom library (driven by this guide):
....
private void MainForm_Load(object sender, EventArgs e)
{
ImageManager.SetImplementation(WinFormsImageManager.Instance);
}
....
// this function is just for example
// real function is bit complicated
private void ShowImage()
{
// Getting DICOM file, retrieving all info from it
// Getting dicomDataset instance
....
var id = dicomDataset.Get<string>(DicomTag.ReferencedFileID, -1);
var dicomImage = new DicomImage(id);
var bitmap = dicomImage.RenderImage().AsBitmap();
pictureBox.Image = bitmap ?? pictureBox.ErrorImage;
}
When image is retrieving all works fine. But as soon as I maximize my MainForm, I got System.ArgumentException with Parameter is not valid message:
It looks like this is a .NET Framework bug, but maybe there is a way to fix it by overrideing OnPaint() method of PictureBox control?
Have anyone see this bug previously?
Thanks in advance.
P.S. During development this project I use following software:
Windows 10 x64
Visual Studio 2017 Community Edition
.NET Framework 4.5.1
fo-dicom version 3.0.2
EDIT #1
The same issue with Panel instead of PictureBox:
You are facing a known and already fixed bug in fo-dicom 3.0.2. See also https://github.com/fo-dicom/fo-dicom/issues/634.
For performance reason the Bitmap, that DicomImage.RenderImage().AsBitmap() returns, does not have its own pixel data, but has a pointer to the bytes of DicomImage. So the AsBitmap() does not duplicate all the pixel data in memory.
But if you instanciate the DicomImage in a local variable and save the Bitmap in the control, then the DicomImage is disposed at the end of the method and the pixel data gets garbace collected. The next time, the Bitmap tries to access the pixel data this exception happens.
The next release will have two methods: AsSharedBitmap() - the same as now but more obvious to the user - and AsClonedBitmap().
The workaround now is, to copy the pixel data manually by calling:
var bitmap = dicomImage.RenderImage().AsBitmap().Clone();

no combination of intermediate filters could be found

I'm making a windows form application using Visual Studio. The application allows you to enter the what you want the photo to be named, and then saves that image to a specific location on the network. It works great when I use it on my laptop. However, when I try to run it on the a desktop, it does not work. Instead I get the message:
System.Runtime.InteropServices.COMException (0x80040217): No
combination of intermediate filters could be found to make the
connection.
at DirectShowLib.DsError.ThrowExceptionForHR(Int32 hr)
at OrderProductCapture.Capture.SetupGraph(DsDevice dev, Int32 iWidth,
Int32 iHeight, Int16 iBPP, Control hControl)
at OrderProductCapture.Capture.ctor(Int32 iDeviceNum, Int32 iWidth,
Int32 iHeight, Int16 iBPP, Control hControl)
at OrderProductCapture.frmMain.ctor()
The Call Stack says:
OrderProductCapture.exe!OrderProductCapture.Capture(int iDeviceNum, int iWidth, int iHeight, short iBPP, System.Windows.Forms.Control hControl) Line 82
OrderProductCapture.exe!OrderProductCapture.frmMain.frmMain() Line 50
OrderProductCapture.exe!OrderProductCapture.Program.Main() Line 19
I have already googled this many times, and I've looked at most of the similar questions on SO. Both computers are using Windows 7 professional. Any help would be fantastic.
This is the code where my code catches the exception. I do not think the code is wrong, because it works fine on my laptop.
public Capture(int iDeviceNum, int iWidth, int iHeight, short iBPP, Control hControl)
{
DsDevice [] capDevices;
// Get the collection of video devices
capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
if (iDeviceNum + 1 > capDevices.Length)
{
throw new Exception("No video capture devices found at that index!");
}
try
{
// Set up the capture graph
SetupGraph( capDevices[iDeviceNum], iWidth, iHeight, iBPP, hControl);
// tell the callback to ignore new images
m_PictureReady = new ManualResetEvent(false);
}
catch
{
Dispose();
throw;
}
}
When having to convert between media formats, you can programmatically force it to use some specific filter chain and configure it tightly to your needs, but DirectSHOW also has the ability of "guessing" the right tools to use. It knows all the tiny media handlers that are oficially installed in the OS, and tries to match them so that final required "conversion" is built.
However, DirectShow still needs those tiny converters to be installed. DS is able to analyze and connect them, but will not provide you any support for exotic media types. Often, even non-exotic can be problematic if the OS is "fresh-n-clean".
If I remember correctly, that error basically means that (on this problematic machine) some "codecs" are missing.
These things often come with any:
drivers for webcams/microphones/soundcards
audio-processing software (sound editors, media recorders, media players, ..)
"codec packs" like CCCP (really, don't get confused by their logo)
specific codec/filter packages
(...)
First thing I'd now do would be:
recall what I tried to convert
try to read all error messages and logs and find out if there's some faulty filter mentioned, maybe it needs reinstalling
compare what audio-related software is installed on machines where the program WORKS versus the problematic machine
basing on the above, try to deduce what codec is missing
find it, download, install
Also, you may read the code of SetupGraph() function. I bet there's a clear reference to the format that is being used, and this may point out what codec is missing.
Codecs also sometimes get damaged (actually not themselves, but their configuration and registration entries may get damaged). If you are sure that the correct codecs are available on the machine, reinstalling or "repairing" (if they have such option) them can help.

Cannot locate resource

I don't know exactly if it is a bug but i am getting all the time runtime IOException error saying Cannot locate resource.
I am loading some images in my app (c#, WPF) in canvas background depends on database state.
The problem is, that i cannot load LAST (alphabeting sorted) file. I have for example 15 images in folder. I can load 14 first without problems. But the last one all the time throw exception.
I am 100% sure, that i HAVE the image in the folder (see !image-printscreen below).
And how i wrote. 14 first i can load without any problem, only the last one throw exception.
Is it a bug in WPF-c# or am I doing something wrong?
code what is throwing exception:
canvas_status.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), #"Images\" + statusName + ".png")));
canvas_name.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), #"Images\" + statusName + bulheadName + ".png")));
error:
IOException was unhalded
Cannot locate resource 'view/images/panel_uzavreno_d.png'.
image (for higher resolution click on image right mouse button and click on SHOW IMAGE or something like that):
I've had a similar issue: IOException, cannot locate a png resource which indeed existed in the assembly.
I found the solution by explicitly specifying the assembly name, even though the caller was in the same assembly as the resource.
Here's how it looks with a Pack URI syntax:
pack://application:,,,/MyAssemblyName;component/MyResourcesFolder/MyImage.png
(For more about Pack URIs see http://msdn.microsoft.com/en-us/library/aa970069.aspx)
Edit: One more thing I had to do after specifying the assembly name was to Clean the project. The problem returns after build but was resolved after cleaning the intermediate products. This is definitely a bug in Visual Studio.
You need to set the build action of the image to the type 'Resource'.
Right-mouse click on the file >> Properties >> set 'Build Action' to 'Resource'

System.Drawing.Icon constructor throwing "Operation completed successfully" exception

On a Windows XP machine, the following code is throwing a System.ComponentModel.Win32Exception with the message "The operation completed successfully"
System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico");
I can stop the program crashing with
try
{
System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico");
}
catch(System.ComponentModel.Win32Exception ex)
{
if (ex.NativeErrorCode != 0)
{
throw;
}
}
but of course the icon is not set.
The full stack trace is
at System.Drawing.Icon.Initialize(Int32 width, Int32 height)
at System.Drawing.Icon..ctor(String fileName, Int32 width, Int32 height)
at System.Drawing.Icon..ctor(String fileName)
at hermes.Window1..ctor() in D:\\projects\\hermesclient\\hermesWPF\\hermes\\Window1.xaml.cs:line 50"
That line 50 is the original line I posted.
This is a WPF app, and on a Windows 7 machine the code works fine.
EDIT: Turned out the icon wasn't working in Windows XP at all, adding 256 colour versions seems to have fixed it.
From the looks of it the problem seems to be an issue with not disposing with objects properly. It's hard to pin point exactly where the issue in your case is occurring but as a general rule of thumb make sure you implement the using directive when dealing with objects that implement IDisposable.
Even in the sample you have provided try doing something like:
using (var icon = new System.Drawing.Icon("icon.ico"))
{
// use icon
}
// icon is then disposed.
Have a read of this article.
Does the file icon1.ico exists in the same directory as the .NET executable? You did not say explicitly...are you reading this as an external icon file? perhaps this
string sPath2Icon = Path.Combine(Environment.CurrentDirectory, "icon1.ico");
using (System.Drawing.Icon icon = new System.Drawing.Icon(sPath2Icon)){
// Do what you have to do with icon!
}
Hope this helps,
Best regards,
Tom.
I had a similar problem. in my case the icon file was a multiicon file containing 32x32, 48x48 and 256x256 size icons. I changed it to a single icon file size 32x32 and it worked fine after that.
Turned out the icon wasn't working in Windows XP at all, adding 256 colour versions seems to have fixed it.

Categories

Resources