Image resize using Gdiplus C++ code and C# code are different

  • Thread starter Thread starter krishna kumar tm
  • Start date Start date
K

krishna kumar tm

Guest
Hi I am working on some image processing project. I need to re-size some of images. I am using C++ with help of GDIPlus libarary. and C# to re-size images.

The re-sized images generated after processing are different with some pixel values. Please let me know what is causing the differences.


C++ Code Sample to generate Image:

// resizeimage.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Gdiplus.h>
#include <iostream>
#include <map>

#pragma comment(lib, "gdiplus.lib")

size_t GetImageEncoder(const wchar_t* form, CLSID* clsID)
{
size_t nRet = -1;
unsigned int encodersCount = 0;
unsigned int encodersSize = 0;
Gdiplus::ImageCodecInfo* imageCodecs = nullptr;

Gdiplus::GetImageEncodersSize(&encodersCount, &encodersSize);

imageCodecs = (Gdiplus::ImageCodecInfo*)malloc(encodersSize);

Gdiplus::GetImageEncoders(encodersCount, encodersSize, imageCodecs);
for (size_t index = 0; index < encodersCount; index++)
{
if (wcscmp(imageCodecs[index].MimeType, form) == 0)
{
*clsID = imageCodecs[index].Clsid;
nRet = index;
break;
}
}

if (imageCodecs)
free(imageCodecs);
return nRet;
}

int _tmain(int argc, _TCHAR* argv[])
{
Gdiplus::GdiplusStartupInputEx gdiStartupInput;
ULONG_PTR gdiplustoken;
Gdiplus::GdiplusStartup(&gdiplustoken, &gdiStartupInput, NULL);
std::map<std::wstring, std::wstring> m_mtMap;
m_mtMap[L".jpeg"] = L"image/jpeg";
m_mtMap[L".jpe"] = L"image/jpeg";
m_mtMap[L".jpg"] = L"image/jpeg";
m_mtMap[L".png"] = L"image/png";
m_mtMap[L".gif"] = L"image/gif";
m_mtMap[L".tiff"] = L"image/tiff";
m_mtMap[L".tif"] = L"image/tiff";
m_mtMap[L".bmp"] = L"image/bmp";

std::wstring strfilePath = L"C:\\TEMP\\PCM\\dups\\1620502_720532344685240_1236226506271750983_n.jpg";
std::wstring strdup = L"C:\\TEMP\\PCM\\dups\\cpp.jpg";
Gdiplus::Bitmap* bitMap = new Gdiplus::Bitmap(strfilePath.c_str());

Gdiplus::Bitmap* sqeezed = new Gdiplus::Bitmap(8, 8, PixelFormat32bppRGB);

Gdiplus::Graphics* graphic = Gdiplus::Graphics::FromImage(sqeezed);
graphic->SetCompositingQuality(Gdiplus::CompositingQuality::CompositingQualityHighQuality);
graphic->SetInterpolationMode(Gdiplus::InterpolationMode::InterpolationModeBilinear);
graphic->SetSmoothingMode(Gdiplus::SmoothingMode::SmoothingModeHighQuality);
graphic->DrawImage(bitMap, 0, 0, 8, 8);
CLSID encoderCLSID;
GetImageEncoder(m_mtMap[L".jpg"].c_str(), &encoderCLSID);
sqeezed->Save(strdup.c_str(), &encoderCLSID);

delete bitMap;
delete sqeezed;
Gdiplus::GdiplusShutdown(gdiplustoken);
return 0;
}

C# Code sample to generate image

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace imageresize
{
class Program
{
static void Main(string[] args)
{
string path = "C:\\TEMP\\PCM\\dups\\1620502_720532344685240_1236226506271750983_n.jpg";
Bitmap bmp = new Bitmap(path);
Bitmap squeezed = new Bitmap(8, 8, PixelFormat.Format32bppRgb);
Graphics canvas = Graphics.FromImage(squeezed);
canvas.CompositingQuality = CompositingQuality.HighQuality;
canvas.InterpolationMode = InterpolationMode.HighQualityBilinear;
canvas.SmoothingMode = SmoothingMode.HighQuality;
canvas.DrawImage(bmp, 0, 0, 8, 8);
squeezed.Save("C:\\TEMP\\PCM\\dups\\cshar.jpg");
}
}
}
C++ Output 8x8

e4be4d18cd52e3257bcc5a968a6f258f._.jpg


C# Output 8x8

a5d3e51e6342dbb47ffc854ff5ebc519._.png


Original file

e602a99dad5a3b171329626ceef96f93._.jpg


Continue reading...
 
Back
Top