Hi, I want to load an image file from computer and resize it.
So I coded like that, but there were problem.
image1 = FixedSize(image1, 300, 300);
I read HELP reference but I didn't understand and I don't know how to solve them.
I used gtk# (and also c#) for a month. Plz give me solution easily for me to understand
Here is my code.
Thanks and sorry for my English:(
private Gtk.Image picturebuf = null;
...
protected void OnBtnOpenClicked (object sender, EventArgs e)
{
FileChooserDialog fcd = new FileChooserDialog ("Open file", this,
FileChooserAction.Open, "Cancel", ResponseType.Cancel,"Open", ResponseType.Accept);
if (fcd.Run () == (int)ResponseType.Accept)
{
if(fcd.Filename.ToLower().EndsWith(".png")||fcd.Filename.ToLower().EndsWith(".bmp")||fcd.Filename.ToLower().EndsWith(".jpg"))
{
if (picturebuf != null && picturebuf.Pixbuf != null)
{
image1 = FixedSize(image1, 300, 300);
image1.Pixbuf.Dispose();
}
image1.Pixbuf = new Gdk.Pixbuf(fcd.Filename);
}
}
fcd.Destroy();
}
static System.Drawing.Image FixedSize (System.Drawing.Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width-(sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height-(sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto, new System.Drawing.Rectangle(destX, destY, destWidth, destHeight),new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
grPhoto.Dispose ();
return bmPhoto;
}
What I have tried:
well, i dont know what i have to write here
i have tried to convert image1(in FixSize func) into image.pixbuf and picturnbuf