[]
DsImaging allows you to load images using Load method of the GcBitmap class. You can load images from file, stream, and byte arrays.
Purpose | Method |
---|---|
Load image from a file | Load (string, System.Drawing.Rectangle?) |
Load image from a stream | Load (System.IO.Stream, System.Drawing.Rectangle?) |
Load image from a byte array | Load (byte[], System.Drawing.Rectangle?) |
To load an image from file, get the image path, store it in a variable and load the file in GcBitmap object using the Load method with the variable as its parameter.
public void LoadSaveFile()
{
//Get the image path
var origImagePath = Path.Combine("Resources", "Images",
"color-woman-postits.jpg");
//Initialize GcBitmap
GcBitmap fileBmp = new GcBitmap();
//Load image from file
fileBmp.Load(origImagePath);
//Add title to image
using (var g = fileBmp.CreateGraphics())
{
var rc = new RectangleF(512, 0, 100, 100);
var tf = new TextFormat
{
Font = Font.FromFile(Path.Combine("Resources", "Fonts",
"times.ttf")),
FontSize = 40
};
g.DrawString("Hello World!", tf, rc, TextAlignment.Center,
ParagraphAlignment.Center, false);
}
//Save image to file
fileBmp.SaveAsJpeg("color-woman-postits-file.jpg");
}
To load an image from stream, instantiate the FileStream class to read the image in the stream and load the file in GcBitmap object using the Load method with FileStream object as its parameter.
public void LoadSaveStream()
{
//Get the image path
var origImagePath = Path.Combine("Resources", "Images",
"color-woman-postits.jpg");
//Initialize GcBitmap
GcBitmap streamBmp = new GcBitmap();
//Load image from stream
FileStream stm = new FileStream(origImagePath, FileMode.Open);
streamBmp.Load(stm);
stm.Close();
//Add title to image
using (var g = streamBmp.CreateGraphics())
{
var rc = new RectangleF(512, 0, 100, 100);
var tf = new TextFormat
{
Font = Font.FromFile(Path.Combine("Resources", "Fonts",
"times.ttf")),
FontSize = 40
};
g.DrawString("Hello World!", tf, rc, TextAlignment.Center,
ParagraphAlignment.Center, false);
}
//Save GcBitmap to stream
MemoryStream outStream = new MemoryStream();
streamBmp.SaveAsJpeg(outStream);
}
To load an image from byte array, you need to read all the bytes of an image using the ReadAllBytes method and load the created byte array in GcBitmap using the Load method.
public void LoadSaveByteArray()
{
//Get the image path
var origImagePath = Path.Combine("Resources", "Images",
"color-woman-postits.jpg");
//Initialize GcBitmap
GcBitmap byteArrayBmp = new GcBitmap();
//Load image from byte array
byte[] imgArray = File.ReadAllBytes(origImagePath);
byteArrayBmp.Load(imgArray);
//Add title to image
using (var g = byteArrayBmp.CreateGraphics())
{
var rc = new RectangleF(512, 0, 100, 100);
var tf = new TextFormat
{
Font = Font.FromFile(Path.Combine("Resources", "Fonts",
"times.ttf")),
FontSize = 40
};
g.DrawString("Hello World!", tf, rc, TextAlignment.Center,
ParagraphAlignment.Center, false);
}
//Save image to file
byteArrayBmp.SaveAsJpeg("color-woman-postits-byteArray.jpg");
}
!type=note
Note: For rendering large or complex text and graphics, you can use Skia library. For more information about the library and its usage, see Render using Skia Library.