Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to load an image file from computer and resize it.
So I coded like that, but there were problem.
C#
image1 = FixedSize(image1, 300, 300);
//compiler error reference: CS1502, CS1503
//cs1503: gtk.image cannot convert system.drawing.image
//cs1502: There are wrong argument at method (am i right? sorry for my eng,,)


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:(

C#
//when button clicked and select an image file, it load through resize function
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();		
		}
//it is resizing function
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
Posted
Updated 4-Oct-16 16:43pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900