LEADTOOLS Support
Imaging
Imaging SDK FAQ
How do I convert from a System.Drawing.Image to a RasterImage?
#1
Posted
:
Wednesday, March 8, 2017 2:55:24 PM(UTC)
Groups: Manager, Tech Support, Administrators
Posts: 218
Was thanked: 12 time(s) in 12 post(s)
The LEADTOOLS Imaging SDK has a RasterImageConverter Class in the Leadtools.Drawing.dll that will help convert to and from a GDI and GDI+ Image object into a LEADTOOLS RasterImage object.
https://www.leadtools.com/help/sdk/dh/ld/rasterimageconverter.htmlThe 2 methods that do the converting are the ConvertoToImage and ConvertFromImage methods:
ConvertToImage Methodhttps://www.leadtools.com/help/sdk/dh/ld/rasterimageconverter-converttoimage.htmlConvertFromImage Methodhttps://www.leadtools.com/help/sdk/dh/ld/rasterimageconverter-convertfromimage.htmlHere is an example of how to Convert from a RasterImage to a GDI+ Image and then back:
C#Code:using(RasterImage srcImage = codecs.Load(srcFileName))
{
// Convert to GDI+ image
ImageIncompatibleReason reason = RasterImageConverter.TestCompatible(srcImage, true);
PixelFormat pf = RasterImageConverter.GetNearestPixelFormat(srcImage);
Console.WriteLine("TestCompatible: {0}", reason);
Console.WriteLine("GetNearestPixelFormat:{0}", pf);
if(reason != ImageIncompatibleReason.Compatible)
{
RasterImageConverter.MakeCompatible(srcImage, pf, true);
}
using(Image destImage1 = RasterImageConverter.ConvertToImage(srcImage, ConvertToImageOptions.None))
{
// Save this image to disk
destImage1.Save(destFileName1, ImageFormat.Bmp);
// Convert the GDI+ image back to a RasterImage
using(RasterImage destImage2 = RasterImageConverter.ConvertFromImage(destImage1, ConvertFromImageOptions.None))
{
// Save it to disk
codecs.Save(destImage2, destFileName2, RasterImageFormat.Bmp, 24);
}
}
}
VBCode:' Load the image
Using srcImage As RasterImage = codecs.Load(srcFileName)
' Convert to GDI+ image
Dim reason As ImageIncompatibleReason = RasterImageConverter.TestCompatible(srcImage, True)
Dim pf As PixelFormat = RasterImageConverter.GetNearestPixelFormat(srcImage)
Console.WriteLine("TestCompatible: {0}", reason)
Console.WriteLine("GetNearestPixelFormat:{0}", pf)
If reason <> ImageIncompatibleReason.Compatible Then
RasterImageConverter.MakeCompatible(srcImage, pf, True)
End If
Using destImage1 As Image = RasterImageConverter.ConvertToImage(srcImage, ConvertToImageOptions.None)
' Save this image to disk
destImage1.Save(destFileName1, ImageFormat.Bmp)
' Convert the GDI+ image back to a RasterImage
Using destImage2 As RasterImage = RasterImageConverter.ConvertFromImage(destImage1, ConvertFromImageOptions.None)
' Save it to disk
codecs.Save(destImage2, destFileName2, RasterImageFormat.Bmp, 24)
End Using
End Using
End Using
Edited by moderator Wednesday, December 27, 2023 2:25:45 PM(UTC)
| Reason: Updated links
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.
LEADTOOLS Support
Imaging
Imaging SDK FAQ
How do I convert from a System.Drawing.Image to a RasterImage?
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.