LEADTOOLS uncompressing example getting an exception - c#

I'm following the next example from the Leadtools page
https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.codecspngoptions.html
The version is the 19
But I'm getting this error in visual studio, {featured not supported}, I don't know what I'm doing wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
namespace DicomTest3
{
public class Program
{
public static void Main(string[] args)
{
const string LEAD_VARS = #"C:\Users\Public\Documents\LEADTOOLSImages";
RasterSupport.SetLicense(
#"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC",
File.ReadAllText(#"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC.KEY")
);
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS, "IMAGE1.CMP");
RasterImage image = codecs.Load(srcFileName);
// save with maximum quality
codecs.Options.Png.Save.QualityFactor = 1;
codecs.Save(image, Path.Combine(LEAD_VARS, "quality.png"), RasterImageFormat.Png, image.BitsPerPixel);
// save with maximum compression
codecs.Options.Png.Save.QualityFactor = 9;
codecs.Save(image, Path.Combine(LEAD_VARS, "compression.png"), RasterImageFormat.Png, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
}
}
Error during debugging

The most likely cause for this error is failing to load the assembly (codec DLL) for the PNG file format, which is Leadtools.Codecs.Png.dll.
You can either add it as a reference in the .NET project, or copy it to the same folder that has your EXE and other LEADTOOLS assemblies such as Leadtools.Codecs.dll
Our demos avoid such problems because they're all built in the BIN sub-folder for the project's platform. For example, if you build a 32-bit .NET 4 demo, its EXE will be placed in this folder:
LEADTOOLS 19\Bin\Dotnet4\Win32
This folder contains all the LEADTOOLS Dotnet4 assemblies for Win32, including the codecs.
The help topic Files to be Included with Your Application details which assemblies are required for different toolkit features.
If this does not solve the problem for you, please provide more details here or open a support case by emailing LEADTOOLS support.

Related

C# .NET Framework 4.8 Class library can't load Bitmap from System.Drawing

My .net Frameowkr Class Library runs on version 4.8, System Drawing reference is Version 4 and System.Drawing.Common is Version 7.
Here is a minimal working prototype:
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestBMP
{
public class Class1
{
public Class1() {
var bitmap = new Bitmap(1000, 2000, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Console.WriteLine("Test");
}
public String hello()
{
return "Hello";
}
}
}
I'm placing a notebook inside the release folder with the following python code to test the library:
from pythonnet import load
load("coreclr")
import clr
clr.AddReference("TestBMP")
from TestBMP import Class1
obj = Class1()
Already there im getting, similar to my main project, this error:
Could not load type 'System.Drawing.Bitmap' from assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral
I have scanned ever single link on google, but all just recommended to add the reference of System.Drawing.Common... I've done that and its also in the release folder of the project. I have also tried different .NET Framework versions without success.

"Failed to initialize the [add-in name] because the assembly [path to an add-in DLL file] does not exist" when launching Revit

I've exhausted every resource possible and can not figure out what the issue is. Button images won't show & keep getting this message when I try to use the command.
Failed to initialize the [add-in name] because the assembly [path to an add-in DLL file] does not exist
when launching Revit. Here's my code that I'm using.
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.CSharp;
using System.Media;
using System.Reflection;
using System.IO.Packaging;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Architecture;
#endregion
namespace TpMechanical
{
internal class App : IExternalApplication
{
public Result OnStartup(UIControlledApplication a)
{
String tabname = "TpMechanical";
String panelname = "Tools";
//Option 1
BitmapImage b1Image = (System.Windows.Media.Imaging.BitmapImage)TpMechanical.Properties.Resources.ResourceManager.GetObject("_design3_fhY_icon.ico");
BitmapImage b2Image = (System.Windows.Media.Imaging.BitmapImage)TpMechanical.Properties.Resources.ResourceManager.GetObject("_design3_fhY_icon.ico");
BitmapImage b3Image = (System.Windows.Media.Imaging.BitmapImage)TpMechanical.Properties.Resources.ResourceManager.GetObject("_design3_fhY_icon.ico");
//Option 2
//Bitmap b1Image = (System.Drawing.Bitmap)(TpMechanical.Properties.Resources.ResourceManager.GetObject("Icon1.ico"));
//Bitmap b2Image = (System.Drawing.Bitmap)(TpMechanical.Properties.Resources.ResourceManager.GetObject("Image1.jpg"));
//Bitmap b3Image = (System.Drawing.Bitmap)(TpMechanical.Properties.Resources.ResourceManager.GetObject("Image2.bmp"));
//Option 3
//BitmapImage b1Image = new BitmapImage(new Uri("pack:application:,,,/TpMechanical/Resources/Icon1.ico"));
//BitmapImage b2Image = new BitmapImage(new Uri("pack:application:,,,/TpMechanical/Resources/Image1.jpg"));
//BitmapImage b3Image = new BitmapImage(new Uri("pack:application:,,,/TpMechanical/Resources/Image2.bmp"));
a.CreateRibbonTab(tabname);
var Tools = a.CreateRibbonPanel(tabname, panelname);
var button1 = new PushButtonData("TpButton1", "Button1", Assembly.GetExecutingAssembly().Location, "TpMechanical.command");
button1.ToolTip = " This is a short description";
button1.LongDescription = "This is a long description \n " +
"this is the second line";
var btn1 = Tools.AddItem(button1);
button1.Image = b1Image;
var button2 = new PushButtonData("TpButton2", "Button2", Assembly.GetExecutingAssembly().Location, "TpMechanical.command2");
button2.ToolTip = " This is a short description";
button2.LongDescription = "This is a long description \n " +
"this is the second line";
button2.Image = b2Image;
var button3 = new PushButtonData("TpButton3", "Button3", Assembly.GetExecutingAssembly().Location, "TpMechanical.command3");
button3.ToolTip = " This is a short description";
button3.LongDescription = "This is a long description \n " +
"this is the second line";
button3.Image = b3Image;
Tools.AddStackedItems(button2, button3);
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication a)
{
return Result.Succeeded;
}
}
}
I also have my manifest code below.
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Command">
<Text>Command TpMechanical</Text>
<Description>Some description for TpMechanical</Description>
<VisibilityMode>AlwaysVisible</VisibilityMode>
<Assembly>C:\My Revit- Custom Files\01-Revit 2021\Revit 2021 Repos\TpMechanical\bin\Debug\TpMechanical.dll</Assembly>
<FullClassName>TpMechanical.Command</FullClassName>
<ClientId>9EDCBEA6-942A-4D9A-932D-612B5E02DC9C</ClientId>
<VendorId>com.typepad.thebuildingcoder</VendorId>
<VendorDescription>The Building Coder, http://thebuildingcoder.typepad.com</VendorDescription>
</AddIn>
<AddIn Type="Command">
<Text>Command TpMechanical</Text>
<Description>Some description for TpMechanical</Description>
<VisibilityMode>AlwaysVisible</VisibilityMode>
<Assembly>C:\My Revit- Custom Files\01-Revit 2021\Revit 2021 Repos\TpMechanical\bin\Debug\TpMechanical.dll</Assembly>
<FullClassName>TpMechanical.Command2</FullClassName>
<ClientId>1A164A1B-8B02-499A-8ADB-94A75557CD66</ClientId>
<VendorId>com.typepad.thebuildingcoder</VendorId>
<VendorDescription>The Building Coder, http://thebuildingcoder.typepad.com</VendorDescription>
</AddIn>
<AddIn Type="Command">
<Text>Command TpMechanical</Text>
<Description>Some description for TpMechanical</Description>
<VisibilityMode>AlwaysVisible</VisibilityMode>
<Assembly>C:\My Revit- Custom Files\01-Revit 2021\Revit 2021 Repos\TpMechanical\bin\Debug\TpMechanical.dll</Assembly>
<FullClassName>TpMechanical.Command3</FullClassName>
<ClientId>C5CEC594-E407-40A8-B1B0-163DAA179CDD</ClientId>
<VendorId>com.typepad.thebuildingcoder</VendorId>
<VendorDescription>The Building Coder, http://thebuildingcoder.typepad.com</VendorDescription>
</AddIn>
<AddIn Type="Application">
<Name>Application TpMechanical</Name>
<Assembly>C:\My Revit- Custom Files\01-Revit 2021\Revit 2021 Repos\TpMechanical\bin\Debug\TpMechanical.dll</Assembly>
<FullClassName>TpMechanical.App</FullClassName>
<ClientId>C12635D2-96E2-4DF4-B172-7BD9487F7AE9</ClientId>
<VendorId>com.typepad.thebuildingcoder</VendorId>
<VendorDescription>The Building Coder, http://thebuildingcoder.typepad.com</VendorDescription>
</AddIn>
</RevitAddIns>
enter image description here
Rereading your question a third time over, it sounds as if your add-in is trying to reference a .NET assembly DLL that cannot be found when Revit tries to load it. Looking at the list of namespaces that you reference in your source code using statements, I see nothing but standard Autodesk Revit, Microsoft and .NET assemblies listed. So, they should all be present and accessible. Are you using anything else elsewhere in your code that is not obvious from that list? You might be able to use tools like fuslogv to analyse your add-in dependencies during load time, as suggested in the note on Exploring Assembly Reference DLL Hell with Fuslogvw.
I suggest you try again with a minimal one-liner external command and a minimal one-liner add-in manifest.
Follow these steps: Revit developers guide add-in registration.
Ensure that Revit has read access to its AddIns folder.
Look at the Hello world walkthrough.
Do not say you exhausted all resources. That would take too long and probably exceed your life span. New resources are being added faster than you can consume them, so any attempt is doomed to fail.
The error message is telling you that the problem is not in the internal implementation code, but just in the basic registration.
Why do you add internal to the IExternalApplication implementation? Isn't that a contradiction? What does that mean?
Why do you use The Building Coder VendorId? That is incorrect. You are not The Building Coder.
Your Assembly path is complex and littered with spaces. In general, I try to avoid such complex paths and all spaces in folder names. I also prefer forward slashes to backward ones. You can omit the folder name entirely if you place the DLL in the same place as the add-in manifest in the AddIns folder.
I am being inundated with similar questions these days. Here is another similar one, a summary of a recent email thread:
[Q] I have dived into the Getting Started with Revit platform API, following the DevTV tutorial by Augusto Goncalves. None of my commands appear on the Revit UI > Add Ins > external commands.
[A] One thing you ought to read is the introductory section of the Revit API developers guide. It tells you exactly what to do to install and launch your add-in. It is shocking of that information is not clear and does not work in the tutorial, though. Thank you for bringing it up!
Installing a Revit add-in is really simple, but people run into difficulties like you describe anyway.
There are only two relevant components:
Add-in manifest file *.addin
.NET class library assembly DLL
These are the important steps:
The DLL must implement IExternalCommand; that means, it must implement the Execute method.
The add-in manifest must point to the DLL and must be placed in the Revit Add-Ins folder for Revit to find and load it.
If the DLL and add-in manifest both reside in the Revit AddIns folder, the full DLL path can be omitted; otherwise it must be specified.
That is really all.
There are thousands of places explaining it; they all say the same thing.
Good luck and lots of fun with the Revit API :-)
[R] I have not had any luck since yesterday about my add-in not appearing in the Revit external commands.
I have carefully structured my code correctly. The add-in manifest file is pointing to my project .dll file. My project class explicitly implements the IExternalCommand interface and fires up the Execute method just fine.
I don't understand what the issue could be, not sure it could be the revit version am using am trying to figure out all possibilities.
[R2] I managed to debug my code. Kindly, ignore previous message.
The location of my manifest add-in file was locked. I guess that was done when my account was set up. The location needed permission to be accessed. This path:
C:\ProgramData\Autodesk\Revit\Addins\2022\
I utilised the try and catch exception to see the issue.
Once I gave access permission, the add-in file is now visible; it worked!

Unity throws an error “CS0246: The type or namespace name ‘SharpKml’ could not be found

I am researching how to generate environments in Unity using GIS data in KML format. I came across the SharpKML plugin and it seems to be ideal for my needs.
However, I am experiencing a strange error in that Unity throws an error
“CS0246: The type or namespace name ‘SharpKml’ could not be found (are you missing a using directive or an assembly reference?)”
The reference is added to VS and I have using SharpKML.Dom and using SharpKML.Engine entries which compile in VS with no problems.
But Unity still throws the error.
I have installed via NuGet and have also downloaded the SharpKML source code and rebuilt the dll on my machine and referenced directly with no change. VS also seems to drop the reference intermittently.
Have you come across this problem before or have any idea what is causing it?
The version of Unity is 2019.1.4f1 and the the version of VS is 2017 running framework 4.7.03062
I have recreated the project on a different machine on a different network and experience the same problem.
using UnityEngine;
using System.IO;
using System.Linq;
using SharpKml.Dom;
using SharpKml.Engine;
public class RenderKML : MonoBehaviour
{
public string KLMPath;
// Start is called before the first frame update
void Start()
{
string kmlPth = "Assets\\kml";
GetKMLFiles(kmlPth);
}
private void GetKMLFiles(string pth)
{
if (pth != null)
{
DirectoryInfo dir = new DirectoryInfo(pth);
FileInfo[] info = dir.GetFiles("*.kml");
foreach (FileInfo f in info)
{
print(f.FullName);
GetKMLData(f);
}
}
}
private void GetKMLData(FileInfo fI)
{
// This will read a Kml file into memory.
Stream fs = new FileStream(fI.FullName, FileMode.Open);
KmlFile file = KmlFile.Load(fs);
Kml kml = file.Root as Kml;
if (kml != null)
{
foreach (var placemark in kml.Flatten().OfType<Placemark>())
{
print(placemark.Name);
}
}
}
}
Every time you press "Run", unity rewrites project files. You cant simply use nuget or add references from external projects. You should download all SharpKML dll files and put them in Assets folder manually. See this for more information: https://answers.unity.com/questions/458300/how-to-use-a-external-dll.html
You need to use the Plugin folder in Unity3D. Download your Package or DLL and put it there.
This link can be helpful

Extracting frames from image in C#

I`ve seen some stackover flow on how to do this but i cant get it to work for myself in visual studio.
What is wrong with code? I have downloaded FFMpeg and im using it as reference. yet, I get the error
"Could not load file or assembly Aforge.Video.FFMPEG. dll or one of
its dependencies"
Here is the code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Video;
using AForge.Video.FFMPEG;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// create instance of video reader
VideoFileReader reader = new VideoFileReader( );
// open video file
reader.Open( "test.avi" );
// read 100 video frames out of it
for ( int i = 0; i < 100; i++ )
{
Bitmap videoFrame = reader.ReadVideoFrame( );
videoFrame.Save(i + ".bmp");
// dispose the frame when it is no longer required
videoFrame.Dispose( );
}
reader.Close( );
}
}
}
The program stop when i click the button and comes with an error.
It looks like the dll might have been moved / deleted (or) probably not checked-in. Usually the best practice is to add them in a library folder (say lib or bin) within your project. So when you check-in your stuff, all the library files get checked in too, and your references are intact.
Since the dll seems to be external, I'm hoping you browsed to it and added the reference.
Can you copy the dll file (preferably within a lib folder) within your project and then add the reference again?
It looks like Visual Studio added the reference but it's not able to find the file.
I'm guessing doing this will solve your problem.
You may need to check the way you build your project as well. Some DLLs may only work under x86 or x64 build configuration. Maybe not the parent dll directly but the references it uses internally.
You need to add to your bin folder the dlls from AForge.NET\Framework\Externals\ffmpeg and make sure you're running 32 bit (project properties -> debug -> platform -> x86

How can I get the icon from the executable file only having an instance of it's Process in C#

I can get the executable location from the process, how do I get the icon from file?
Maybe use windows api LoadIcon(). I wonder if there is .NET way...
Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);
This is a sample from a console application implementation.
using System;
using System.Drawing; //For Icon
using System.Reflection; //For Assembly
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
try
{
//Gets the icon associated with the currently executing assembly
//(or pass a different file path and name for a different executable)
Icon appIcon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}
catch(ArgumentException ae)
{
//handle
}
}
}
}
Use the ExtractIconEx (and here) p/invoke. You can extract small and large icons from any dll or exe. Shell32.dll itself has over 200 icons that are quite useful for a standard Windows application. You just have to first figure out what the index is for the icon(s) you want.
Edit: I did quick SO search and found this. The index 0 icon is the application icon.

Categories

Resources