locked
Converting and loading in parallel DICOM files to PNG RRS feed

  • Question

  • User1289604957 posted

    Hello,

    how can I convert and load some png files in one shot? The following code only convert some medical images to png files when clicking on Load button, but I have to refresh the whole application to load all the png files:

    @page "/"
    
    @using System.IO
    @using Dicom.Imaging
    @using SixLabors.ImageSharp
    @using SixLabors.ImageSharp.PixelFormats
    
        <div>
            <button @onclick="ConvertToPngAsync">Load</button>
    
            @foreach (var i in ImageNames)
            {
                <div><img class="border" src="@i" alt="image"></div>
            }
    
        </div>
    
    @code
    {
        protected IEnumerable<String> ImageNames = Enumerable.Range(0, 87).Select(x => $"img/{x}.png");
    
        async Task ConvertToPngAsync()
        {
            var fullPath = Path.GetFullPath(Environment.CurrentDirectory);
            var imgDir = Path.Combine(fullPath, "wwwroot", "img");
            var dcmDir = Path.Combine(fullPath, "wwwroot", "dcm");
            ImageManager.SetImplementation(new RawImageManager());
    
            for (int i = 0; i < 87; i++)
            {
                var dcm = new DicomImage(@$"{dcmDir}\{i:000000}.dcm");
                var bytes = (dcm.RenderImage() as RawImage).AsBytes();
                var bmp = Image.LoadPixelData<Bgra32>(Configuration.Default, bytes, dcm.Width, dcm.Height);
                bmp.Save($@"{imgDir}\{i}.png");
            }
            await InvokeAsync(() => this.StateHasChanged());
        }
    }
    

    Best regards,

    Maher

    Wednesday, December 25, 2019 3:49 PM

Answers

  • User1289604957 posted
    @using System.IO
    @using Dicom.Imaging
    @using SixLabors.ImageSharp
    @using SixLabors.ImageSharp.PixelFormats
    
        <div>
            @foreach (var i in ImageNames)
            {
                <div><img class="border" src="@i" alt="image"></div>
            }
        </div>
    
    @code
    {
        protected IEnumerable<String> ImageNames = Enumerable.Range(0, 87).Select(x => $"img/{x}.png");
    
        protected override Task OnInitializedAsync()
        {
            var fullPath = Path.GetFullPath(Environment.CurrentDirectory);
            var imgDir = Path.Combine(fullPath, "wwwroot", "img");
            var dcmDir = Path.Combine(fullPath, "wwwroot", "dcm");
            ImageManager.SetImplementation(new RawImageManager());
    
            for (int i = 0; i < 87; i++)
            {
                var dcm = new DicomImage(@$"{dcmDir}\{i:000000}.dcm");
                var bytes = (dcm.RenderImage() as RawImage).AsBytes();
                var bmp = Image.LoadPixelData<Bgra32>(Configuration.Default, bytes, dcm.Width, dcm.Height);
                bmp.Save($@"{imgDir}\{i}.png");
            }
    
            return base.OnInitializedAsync();
        }
    
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 25, 2019 11:28 PM
  • User1289604957 posted
    @using System.IO
    @using Dicom.Imaging
    @using SixLabors.ImageSharp
    @using SixLabors.ImageSharp.PixelFormats
    
    
    <div>@foreach (var i in ImageNames){<img class="border" src="@i" alt="image">}</div>
    
    @code
    {
        protected IEnumerable<String> ImageNames = Enumerable.Range(0, 102).Select(x => $"img/{x}.png");
    
        protected override Task OnInitializedAsync()
        {
            var fullPath = Path.GetFullPath(Environment.CurrentDirectory);
            var imgDir = Path.Combine(fullPath, "wwwroot", "img");
            if (Directory.GetFiles(imgDir).Count() != 102)
            {
                var dcmDir = Path.Combine(fullPath, "wwwroot", "dcm");
                ImageManager.SetImplementation(new RawImageManager());
                for (int i = 0; i < 102; i++)
                {
                    var dcm = new DicomImage(@$"{dcmDir}\{i:000000}.dcm");
                    using (var img = (dcm.RenderImage() as RawImage))
                    {
                        using (var bmp = Image.LoadPixelData<Bgra32>(Configuration.Default, img.AsBytes(), dcm.Width, dcm.Height))
                        {
                            bmp.Save($@"{imgDir}\{i}.png");
                        }
                    }
                }
            }
            return base.OnInitializedAsync();
        }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 28, 2019 5:53 PM

All replies

  • User1289604957 posted
    @using System.IO
    @using Dicom.Imaging
    @using SixLabors.ImageSharp
    @using SixLabors.ImageSharp.PixelFormats
    
        <div>
            @foreach (var i in ImageNames)
            {
                <div><img class="border" src="@i" alt="image"></div>
            }
        </div>
    
    @code
    {
        protected IEnumerable<String> ImageNames = Enumerable.Range(0, 87).Select(x => $"img/{x}.png");
    
        protected override Task OnInitializedAsync()
        {
            var fullPath = Path.GetFullPath(Environment.CurrentDirectory);
            var imgDir = Path.Combine(fullPath, "wwwroot", "img");
            var dcmDir = Path.Combine(fullPath, "wwwroot", "dcm");
            ImageManager.SetImplementation(new RawImageManager());
    
            for (int i = 0; i < 87; i++)
            {
                var dcm = new DicomImage(@$"{dcmDir}\{i:000000}.dcm");
                var bytes = (dcm.RenderImage() as RawImage).AsBytes();
                var bmp = Image.LoadPixelData<Bgra32>(Configuration.Default, bytes, dcm.Width, dcm.Height);
                bmp.Save($@"{imgDir}\{i}.png");
            }
    
            return base.OnInitializedAsync();
        }
    
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 25, 2019 11:28 PM
  • User1289604957 posted
    @using System.IO
    @using Dicom.Imaging
    @using SixLabors.ImageSharp
    @using SixLabors.ImageSharp.PixelFormats
    
    
    <div>@foreach (var i in ImageNames){<img class="border" src="@i" alt="image">}</div>
    
    @code
    {
        protected IEnumerable<String> ImageNames = Enumerable.Range(0, 102).Select(x => $"img/{x}.png");
    
        protected override Task OnInitializedAsync()
        {
            var fullPath = Path.GetFullPath(Environment.CurrentDirectory);
            var imgDir = Path.Combine(fullPath, "wwwroot", "img");
            if (Directory.GetFiles(imgDir).Count() != 102)
            {
                var dcmDir = Path.Combine(fullPath, "wwwroot", "dcm");
                ImageManager.SetImplementation(new RawImageManager());
                for (int i = 0; i < 102; i++)
                {
                    var dcm = new DicomImage(@$"{dcmDir}\{i:000000}.dcm");
                    using (var img = (dcm.RenderImage() as RawImage))
                    {
                        using (var bmp = Image.LoadPixelData<Bgra32>(Configuration.Default, img.AsBytes(), dcm.Width, dcm.Height))
                        {
                            bmp.Save($@"{imgDir}\{i}.png");
                        }
                    }
                }
            }
            return base.OnInitializedAsync();
        }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 28, 2019 5:53 PM