Change resolution of the image in binary form

Hello,

I have images (.jpg) in my database in varbinary form.
Part of the varbinary example:
0xFFD8FFE000104A46494600010101004800480000FFE1214E457…

…this image has resolution e.g. 1000x500

and using my C# script I need to change this resolution divide by 2. So the goal is to have 500x250.

If I have classic .jpg file in my folder I can easily edit resolution by following methods from the link:
https://reference.aspose.com/imaging/net/aspose.imaging/resizetype/

Can you help me how to solve it with varbinary form? Any tips and tricks? :slight_smile:

Thank you very much

@john95 Thank you for your interest in our product. Aspose.Imaging allows you to load images from either a file or a stream. You can try to write the binary data you have to a MemoryStream and then load a RasterImage from this stream and once you have loaded your image, you can easily resize it using the methods outlined in the documentation you mentioned. Hope this helps.

1 Like

@Alexey.Karpenko Thank you very much Alexey!
It’s working, but occurred another question related to this:
After successful converting to byte array to MemoryStream I tried to use Bilinear/Neighbour resampling algorithm and I generated (saved) new jpg picture into file system. But if I open this new jpg file, then I can see only part of the picture and rest of it is black. So it seems that it worked only in the beginning and then it stopped.

Sample of my C# code:

public static void LoadImage()
{
string varbinaryString = “0xFFD8FFE000104A46494600010101004800480000FFE1214E4578696600004D4D002A…”;
var no_0x_varbinary_str = varbinaryString.Replace(“0x”, “”).Trim();
byte[] bytes = Enumerable.Range(0, no_0x_varbinary_str.Length / 2)
.Select(x => Convert.ToByte(no_0x_varbinary_str.Substring(x * 2, 2), 16))
.ToArray();
using (MemoryStream ms = new MemoryStream(bytes))
{
Aspose.Imaging.LoadOptions options = new LoadOptions();
using (Aspose.Imaging.Image asposeImage = Aspose.Imaging.Image.Load(ms))
{
asposeImage.Resize(asposeImage.Width / 2, asposeImage.Height / 2, Aspose.Imaging.ResizeType.BilinearResample);
asposeImage.Save(dir + “OutcomeBilinear.jpg”);
}
}
}

and in Main I just call this LoadImage method (after using valid Aspose licence of course).
But result is incomplete image. (I can see 5% of the image, rest is black)

Do you know what is wrong?

Thank you in advance!

@john95 Could you please provide the varbinary string saved to file so that we can test it? We will provide you with a solution as soon as possible.

@Alexey.Karpenko
Sure, I’m sending a zip file, where is txt file with varbinary string which I’m trying resample.

20230613_VarbinaryImage.zip (13.6 KB)

Thank you in advance!

@john95 Thanks, we’ll test it and get back to you soon with the results.

1 Like

Hello @Alexey.Karpenko ,

can I ask you if you have some progress with my issue?

Thank you in advance,
Have a nice day!

Hello @john95,
We have investigated this issue and it seems that there is a problem with the image in its varbinary form. You can check this by saving the image byte array using File.WriteAllBytes("OutcomeBilinear.jpg", bytes);.

This image only contains the top 5% of its original content.