Preparing a DICOMweb request or response starts with choosing the content representation your endpoint expects. You may need content generated from a DICOM file, or a dataset serialized as DICOM JSON. These representations use different media types, so the choice needs to match the operation you are implementing.
In this tutorial, you’ll use Aspose.Medical for .NET to generate both representations in C#. You’ll load a local DICOM file, create content with DicomWebContent.FromDicomFile, and use DicomWebContent.FromDataset to create DICOM JSON content from its dataset. Each example prints the resulting content type so you can inspect it before integrating the content into an HTTP workflow.
Install Aspose.Medical for .NET
Add Aspose.Medical for .NET to your project using Visual Studio’s Package Manager Console:
Install-Package Aspose.Medical
Alternatively, run the following command from your project directory:
dotnet add package Aspose.Medical
Use a package version that includes the Aspose.Medical.Dicom.Web APIs shown below. Confirm your target framework against the requirements for that package version rather than assuming that every release supports the same frameworks.
Before running either example, place a readable DICOM file named image.dcm in your application’s working directory, or replace the relative path with your file’s full path. These examples operate locally; you do not need a DICOMweb server to create the content objects.
If you need to evaluate the API with a license, you can request a temporary license. The snippets focus on content generation and do not include license initialization.
Create DICOMweb Content from a DICOM File
Use DicomWebContent.FromDicomFile when you need a content representation of a loaded DICOM file.
- Open the source file with
DicomFile.Open. - Pass the resulting
DicomFiletoDicomWebContent.FromDicomFile. - Read
ContentTypeto inspect the generated content’s media type.
using System.Net.Http.Headers;
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Web;
// Create content from a single DICOM file
DicomFile file = DicomFile.Open("image.dcm");
DicomWebContent fileContent = DicomWebContent.FromDicomFile(file);
System.Console.WriteLine($"Content type: {fileContent.ContentType}");
DicomFile.Open("image.dcm") loads the source file. FromDicomFile(file) then creates its binary DICOM content representation. The media type for this representation is application/dicom.
This example creates the content object and writes its content type to the console. It does not save a new DICOM file or send the content to a server.
Generate DICOMweb JSON Content from a Dataset
Use DicomWebContent.FromDataset when you want to create content from an in-memory dataset. For DICOM JSON, specify application/dicom+json using DicomWebMediaTypes.ApplicationDicomJson.
The input is a Dataset, not an already serialized JSON string. In this example, the dataset comes from the same local DICOM file:
- Load
image.dcmand accessfile.Dataset. - Create a
MediaTypeHeaderValuefor DICOM JSON. - Pass the dataset and media type to
DicomWebContent.FromDataset. - Inspect the resulting
ContentType.
using System.Net.Http.Headers;
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Web;
// Assume 'file' is a DicomFile loaded earlier
DicomFile file = DicomFile.Open("image.dcm");
Dataset dataset = file.Dataset;
// Create content from a dataset serialized as DICOM JSON
DicomWebContent jsonContent = DicomWebContent.FromDataset(
dataset,
new MediaTypeHeaderValue(DicomWebMediaTypes.ApplicationDicomJson));
System.Console.WriteLine($"JSON content type: {jsonContent.ContentType}");
file.Dataset provides the dataset used to create the JSON representation. The call to FromDataset supplies both that dataset and the requested media type; you do not need a separate JSON-serialization step in this example.
The two snippets are independent. Each loads its own file and declares a variable named file. If you combine them into one program, keep the using directives at the top and reuse the existing file variable.
Choose the Content Representation for Your HTTP Workflow
Choose the method according to the representation required by your DICOMweb operation:
| Input and intended representation | Method | Media type |
|---|---|---|
| Loaded DICOM file as binary DICOM content | DicomWebContent.FromDicomFile(file) | application/dicom |
| In-memory dataset as DICOM JSON content | DicomWebContent.FromDataset(dataset, mediaType) | application/dicom+json |
Creating either object is a local preparation step. It does not upload the source file, execute a query, or complete a DICOMweb transaction.
When integrating the content into an HTTP request or response, follow the API and media-type requirements of the operation you are implementing. The snippets do not demonstrate conversion to HttpContent, multipart packaging, authentication, or sending a request with HttpClient.
Run the Examples and Check the Results
Run either snippet in a C# console application after installing the package and making image.dcm available. If you are using a console project, place one snippet in Program.cs and run:
dotnet run
The file-based example prints a line beginning with Content type:. The dataset example prints a line beginning with JSON content type:. Check the displayed media types against the representation you requested:
| Example | Expected media type |
|---|---|
| DICOM file content | application/dicom |
| Dataset serialized as DICOM JSON | application/dicom+json |
These are the expected media-type values, not a transcript from an executed test. The examples print content headers; they do not print the binary payload or serialized JSON.
Troubleshoot DICOMweb Content Generation
Check the input file path
The relative path image.dcm is resolved from the application’s working directory. If the file cannot be opened, check its location, access permissions, and whether it is a readable DICOM file. Use an absolute path if the working directory differs between your IDE and command-line runs.
Confirm the installed package exposes the required APIs
If Aspose.Medical.Dicom.Web or DicomWebContent cannot be resolved, check the installed package version and its API availability. Adding a using directive does not make an API available when the referenced package does not contain it.
Keep the dataset and JSON inputs distinct
The dataset example passes file.Dataset directly to FromDataset. Do not replace it with a JSON string and assume the same overload accepts that input. The media-type argument specifies the representation being created.
Check the destination operation before sending content
A content object with the expected media type is only part of an HTTP workflow. Confirm the destination endpoint, request method, headers, authentication, and any multipart requirements before adding a send operation. A successful local content-generation call does not confirm that a server will accept the eventual request.
Conclusion
Use FromDicomFile for content generated from a DICOM file and FromDataset with application/dicom+json for a DICOM JSON representation. Run the relevant snippet with your own file, inspect ContentType, and then integrate the content into the request or response required by your endpoint.
See the Aspose.Medical documentation and API reference for related configuration and API details.
Frequently Asked Questions
What is the difference between the two content-generation methods?
FromDicomFile creates content from a loaded DICOM file. FromDataset creates a content representation from an in-memory dataset using the supplied media type. This tutorial uses the latter to create DICOM JSON content.
Do I need to serialize the dataset to JSON first?
No. The example passes a Dataset to FromDataset with the application/dicom+json media type. It does not take an existing JSON string as input.
Which media type is used for DICOM JSON content?
Use application/dicom+json, represented in the example by DicomWebMediaTypes.ApplicationDicomJson.
Do these examples upload a DICOM file to a server?
No. They create content objects locally and print their content types. Sending the content requires additional HTTP integration code.
Can I combine the two examples in one program?
Yes. Keep the using directives at the top and reuse the loaded DicomFile instead of declaring the file variable twice in the same scope.
