MPEG-7 descriptors - c#

I am trying to compare images by MPEG7 descriptors and I found a implementation by http://chatzichristofis.info/?page_id=19 but when I call Apply() function so the exception is thrown.
EHD_Descriptor ehd = new EHD_Descriptor(11);
var img = new Bitmap("LargerImage.jpg");
.
.
.
descriptor = ehd.Apply(img);
descriptor = ehd.Quant(descriptor);
Exception
System.AccessViolationException was unhandled
HResult=-2147467261
Message=...
Does anyone has any experiance with these descriptors or any reference to other descriptors which works without exceptions (from unmanaged code, I think)?

If someone has a same problem and just need to proccess other images, so one solution is:
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
private double[] DoSecurityCritical(SimpleRnd.CEDD cedd, Bitmap img) {
try {
// List<double[]> temp = locate.extract(img,600);
double [] temp = cedd.Apply(img);
return temp;
} catch (Exception) {
return null;
}
}
Just use stronger catch with security attributes.

Related

Saving Binary Data FHIR DB

I am trying to save Binary data to FHIR DB.
This is my method:
public static Patient SavePdfForms(string resource, HttpClientEventHandler messageHandler, string[] pdfForms, Patient patient, FhirClient BinaryBundleClient)
{
Bundle BinaryBundle = new Bundle();
BinaryBundle.Type = Bundle.BundleType.Collection;
try
{
foreach (var item in pdfForms)
{
Binary BinaryData = new Binary();
var bytearray = Encoding.ASCII.GetBytes(item);
BinaryData.Data = bytearray;
BinaryData.ContentType = "application/fhir+json";
var binaryResource = BinaryBundleClient.Create(BinaryData);
BinaryBundle.AddResourceEntry(BinaryData, resource + "/BundleResource/" + binaryResource.Id);
}
}
catch (Exception ex)
{
throw;
}
var bundleId = BinaryBundleClient.Create(BinaryBundle);
patient.Identifier.Add(new Identifier("BinaryBundle", bundleId.Id));
return BinaryBundleClient.Update(patient);
}
The string[] of pdfForms is base64 and for each form I am creating a new binary and adding data and content type. But the line var binaryResource = BinaryBundleClient.Create(BinaryData); throws an error and data is not a valid json. I tried with different content type but that is not working. Any ideas why?
Assuming you are creating a new resource instance in BinaryBundleClient.Create(BinaryData) and the server to store it.
In your case, you directly pass the binary information in Fhirclient.Create(your data)
binaryResource = BinaryBundleClient.Create(BinaryData);
You must mention the type of the resource instance which follows:
Create Fhir client
var client = new FhirClient("http://server.fire.ly");
After Creating FhirClient You have to create a new resource instance. It will be like
var pat = new Patient() { /* set up data */ };
var created_pat = client.Create<Patient>(pat);
this interaction will throw an Exception when things go wrong, in most cases a FhirOperationException. This exception has an Outcome property that contains an OperationOutcome resource, and which you may inspect to find out more information about why the interaction failed. Most FHIR servers will return a human-readable error description in the OperationOutcome to help you out.
Refer here

COMAdminCatalogCollection.Populate throws error C#

I am using the COMAdminCatalog API for .net. I am currently getting certain properties of COM+ applications and components from an array of servers remotely. For the most part, my process doesn't have a problem retrieving this data. However, a few of the COM+ application's components will not populate and throws the following exception:
Error Message: Errors occurred accessing one or more objects - the ErrorInfo collection may have more detail (Exception from HRESULT: 0x80110401)
I try getting the ErrorInfo collection, but no errorInfo objects are returned, so that doesn't help me in trouble shooting.
Here is the documentation:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686530(v=vs.85).aspx
Here is the method I am using to try to populate a component collection. Any help would be greatly appreciated. Thanks in advance
private static bool TryPopulateComponent(COMAdminCatalogCollection cOMAdminCatalogComponentCollection,
COMAdminCatalogObject application = null, COMAdminCatalogObject component = null, string serverName = null)
{
try
{
cOMAdminCatalogComponentCollection.Populate();
return true;
}
catch (Exception ex)
{
COMAdminCatalogCollection errorInfos = cOMAdminCatalogComponentCollection.GetCollection("ErrorInfo", varObjectKey: application.Key);
errorInfos.Populate();
foreach (COMAdminCatalogObject errorInfo in errorInfos)
{
var errorCode = errorInfo.Value["ErrorCode"];
var majorRef = errorInfo.Value["MajorRef"];
var minorRef = errorInfo.Value["MinorRef"];
var name = errorInfo.Value["Name"];
}
COMException comException = new COMException(ex.Message, ex.InnerException,
application, component, serverName);
Console.WriteLine($"Could not populate {component?.Name}");
Console.WriteLine(ex.Message);
LogTools.LogError(comException);
return false;
}
}

Multitexturing with C# and ActiViz .NET

I want to use ActiViz .NET and C# to multitexture an object created from .obj file.
As long as I know somehow to use texture with single texture, I have problem with more than one texture. Based on what I found on VTK GitHub I started writing a code, but there comes problem with SetBlendingMode and MapDataArrayToMultiTextureAttribute methods.
I use SetBlendingMode like this:
texture.SetBlendingMode(vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_REPLACE);
which results with:
The best overloaded method match for 'Kitware.VTK.vtkTexture.SetBlendingMode(int)' has some invalid arguments
With MapDataArrayToMultiTextureAttribute it looks like this:
mapper.MapDataArrayToMultiTextureAttribute(vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, "TCoords", vtkDataObject.FIELD_ASSOCIATION, 0);
which ends with similar message:
The best overloaded method match for 'Kitware.VTK.vtkPolyDataMapper.MapDataArrayToMultiTextureAttribute(int, string, int, int)' has some invalid arguments
The whole code:
private void ReadOBJ(string path)
{
vtkTesting test = vtkTesting.New();
// Varibles for obj file and texture
string filePath = path;
string texturePath0 = #"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_0.jpg";
string texturePath1 = #"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_1.jpg";
string texturePath2 = #"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_2.jpg";
string texturePath3 = #"C:\Users\admin\Desktop\Fox-Skull-obj\Fox Skull_3.jpg";
// Open jpeg file including texture
vtkJPEGReader jpegReader = new vtkJPEGReader();
jpegReader.SetFileName(texturePath0);
jpegReader.Update();
vtkJPEGReader jpegReader1 = new vtkJPEGReader();
jpegReader1.SetFileName(texturePath1);
jpegReader1.Update();
vtkJPEGReader jpegReader2 = new vtkJPEGReader();
jpegReader2.SetFileName(texturePath2);
jpegReader2.Update();
vtkJPEGReader jpegReader3 = new vtkJPEGReader();
jpegReader3.SetFileName(texturePath3);
jpegReader3.Update();
// Open obj file
vtkOBJReader reader = new vtkOBJReader();
if (!File.Exists(filePath))
{
MessageBox.Show("Cannot read file \"" + filePath + "\"", "Error", MessageBoxButtons.OK);
return;
}
reader.SetFileName(filePath);
reader.Update();
vtkTriangleFilter triangleFilter = vtkTriangleFilter.New();
triangleFilter.SetInputConnection(reader.GetOutputPort());
vtkStripper stripper = vtkStripper.New();
stripper.SetInputConnection(triangleFilter.GetOutputPort());
stripper.Update();
vtkPolyData polydata = stripper.GetOutput();
polydata.Register(null);
polydata.GetPointData().SetNormals(null);
vtkFloatArray TCoords = vtkFloatArray.New();
TCoords.SetNumberOfComponents(2);
TCoords.Allocate(8, 0);
TCoords.InsertNextTuple2(0.0, 0.0);
TCoords.InsertNextTuple2(1.0, 0.0);
TCoords.InsertNextTuple2(0.0, 1.0);
TCoords.InsertNextTuple2(1.0, 1.0);
TCoords.SetName("TCoords");
polydata.GetPointData().AddArray(TCoords);
// Create texture
vtkTexture texture = new vtkTexture();
vtkTexture texture1 = new vtkTexture();
vtkTexture texture2 = new vtkTexture();
vtkTexture texture3 = new vtkTexture();
texture.SetInputConnection(jpegReader.GetOutputPort());
texture1.SetInputConnection(jpegReader1.GetOutputPort());
texture2.SetInputConnection(jpegReader2.GetOutputPort());
texture3.SetInputConnection(jpegReader3.GetOutputPort());
texture.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_REPLACE);
texture1.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_ADD);
texture2.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_ADD);
texture3.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_ADD);
// Mapping textures
vtkTextureMapToCylinder mapSphere = new vtkTextureMapToCylinder();
mapSphere.SetInputConnection(reader.GetOutputPort());
// Visualize
vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
mapper.SetInput(polydata);
// Get a reference to the renderwindow of our renderWindowControl1
vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
// Renderer
vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
vtkRenderWindowInteractor iren = vtkRenderWindowInteractor.New();
iren.SetRenderWindow(renderWindow);
// Create actor and add mapper with texture
vtkActor actor = vtkActor.New();
vtkOpenGLHardwareSupport hardware = vtkOpenGLRenderWindow.SafeDownCast(renderWindow).GetHardwareSupport();
bool supported = hardware.GetSupportsMultiTexturing();
int tu = 0;
if (supported)
{
tu = hardware.GetNumberOfFixedTextureUnits();
}
if (supported && tu > 2)
{
mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);
mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_1, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);
mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_2, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);
mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_3, "TCoords", (int)vtkDataObject.FieldAssociations.FIELD_ASSOCIATION_POINTS, -1);
actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, texture);
actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_1, texture1);
actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_2, texture2);
actor.GetProperty().SetTexture((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_3, texture3);
}
else
{
if (supported)
{
mapper.MapDataArrayToMultiTextureAttribute((int)vtkProperty.VTKTextureUnit.VTK_TEXTURE_UNIT_0, "TCoords", (int)vtkDataObject.AttributeTypes.POINT, 0);
}
actor.SetTexture(texture);
}
actor.SetMapper(mapper);
renderWindow.AddRenderer(renderer);
// Hide current actor
renderer.RemoveAllViewProps();
// Set background color
renderer.SetBackground(0.3, 0.6, 0.3);
// Add new actor to the renderer
renderer.AddActor(actor);
// Render
renderWindow.Render();
}
Does anybody have any experience with multitexture with C# and VTK and can help me? Do you know any other solutions that can help me?
EDIT 01.12.2015
Thanks to JohnnyQ answer I probably make functions mentioned above working. I say "probably" because right now when I'm running code and select .obj file program stops working with one of two errors:
An unhandled exception of type 'System.AccessViolationException' occurred in Kitware.VTK.dll with information about attempt of read or write protected memory or program just stops with vshost32.exe stop working message.
I've updated code above to actual version. Any suggestions are still appreciated.
You have to explicit cast from the enum type to an integral type.
e.g.
texture.SetBlendingMode((int)vtkTexture.VTKTextureBlendingMode.VTK_TEXTURE_BLENDING_MODE_REPLACE);

Try-catch creating new array in C# - where to initialize it?

If I'm reading a string from a config file, I'd use a similar approach to the below, in case the string isn't present in the file being read and an exception results. However, if I want to do the same for a string[] array, I can't just 'new it up' outside the try block because the size is not known.
I can't new it up in the try block itself. How should it be approached?
string[] logContent; // can't new it up here as don't know the size
try
{
logContent = File.ReadAllLines(aLogFile);
}
catch
{
throw new Exception("LoggerStandard: Specified Logfile exists but could not be read.");
}
You could initialize it to a default value:
string[] logContent = null;
try
{
logContent = File.ReadAllLines(aLogFile);
}
catch
{
// Be careful with the error message here => there might be other reasons
// that the ReadAllLines threw an exception
throw new Exception("LoggerStandard: Specified Logfile exists but could not be read.");
}
You can initialize it with null and then check against it.
By default it is null. You can leave it as is, if this is appropriate for your program, or initialize to any array according to your needs. Anyway, successful initialization inside of try block overrides this.
string[] logContent=null;
try
{
logContent = File.ReadAllLines(aLogFile);
}
catch
{
throw new Exception("LoggerStandard: Specified Logfile exists but could not be read.");
}

RichTextBox lifetime in static method context

Please take a look at the method depicted below
public static string RemoveRTF(string input)
{
string output = input;
RichTextBox RichTextBox1 = new RichTextBox();
try {
RichTextBox1.Rtf = input;
output = RichTextBox1.Text;
} catch (ArgumentException argExp) {
/*
* The supplied input value is not in RTF format.
* Ignore.
*/
}
return output;
}
My question is, will the above code when called several times generate a large amount of USER Objects, Handles Or GDI Objects.
The reason for asking is that I have some code which worked perfectly one day and then the next day without any code changes made stopped working with the reported error :
Error creating Window Handle..
Only thing is that I cant seem to see the cause for the problem except that the callstack shows me that the error originates in the above code.
TaskManager do not reveal a large amount of USER objects or such being created, so I really do not know what is going on.
You should dispose your RichTextBox to free up any unmanaged resources.
RichTextBox1.Dispose();
or you can make one global RichTextBox and use it.
RichTextBox RichTextBox1 = new RichTextBox();
public static string RemoveRTF(string input)
{
string output = input;
try {
RichTextBox1.Rtf = input;
output = RichTextBox1.Text;
RichTextBox1.rtf = null;
} catch (ArgumentException argExp) {
/*
* The supplied input value is not in RTF format.
* Ignore.
*/
}
return output;
}
or use using()
The RichTextBox object only works on the UI thread. Calling this code from the background thread will throw an Exception with a message like "Error creating Window Handle.."
I also received the same error that u r facing "Error creating window handle". This issue occurs because even if we create object of RichTextBox and and set that object to null at the end of the method, it does not get disposed and so initially it works fine and then later on it start giving "error creating window handle". So instead use "Using". It will dispose object of richTextBox outside "using" context. This will solve that error.
private String RemoveRtf(String RtfScript)
{
string PlainText = null;
try
{
if (!String.IsNullOrEmpty(RtfScript))
{
using (RichTextBox richTxtBox = new RichTextBox())
{
richTxtBox.Rtf = RtfScript;
PlainText = richTxtBox.Text;
}
}
}
catch (Exception ex)
{
// log error here
}
finally
{
RtfScript = null;
}
return PlainText;
}

Categories

Resources