Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 9th, 2005, 11:20 AM   #1
Ghost
Man Bear Pig Hunter
 
Ghost's Avatar
 
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4 Ghost is on a distinguished road
Create BMP

Just wondering how someone would go about converting an image to make a BMP version of any png or jpg image uploaded. I just need to work the convert code. Thanks.
Ghost is offline   Reply With Quote
Old Nov 9th, 2005, 11:56 AM   #2
Ghost
Man Bear Pig Hunter
 
Ghost's Avatar
 
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4 Ghost is on a distinguished road
Well I got that code down, but anyone know of a good logical way to resize keeping the correct proportions. Such as 500x250 resized to fit the max width accepted of 300 would then become 300x150.

Here is what I have....

double ratio = ImageLogo.Width.Value / 475;
ImageLogo.Width = ImageLogo.Width.Value * ratio;
Ghost is offline   Reply With Quote
Old Nov 9th, 2005, 7:22 PM   #3
bja888
Hobbyist Programmer
 
bja888's Avatar
 
Join Date: Oct 2005
Location: Central, FL
Posts: 213
Rep Power: 0 bja888 is an unknown quantity at this point
Send a message via AIM to bja888 Send a message via Yahoo to bja888
Hey Ghost, aparently there isn't anyone here who knows how to deal woth images in C# as I assume form the lack of replays to your comment and mine about pngs.

I destroyed my brian one night comeing up with a code to re-size based on a max width or height while keeping aspect ratio. I really don't want to just give it away but since no one else will be able to help even a little bit I will post it.
I can not right now due to a computer proublem I had yeasterday but as soon as I can get access tot he code again I will put it up for you. Shouldent be more than 2 days.
bja888 is offline   Reply With Quote
Old Nov 9th, 2005, 7:38 PM   #4
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
Quote:
I destroyed my brian one night comeing up with a code to re-size based on a max width or height while keeping aspect ratio. I really don't want to just give it away but since no one else will be able to help even a little bit I will post it.
That's not difficult at all and should not be guarded as if it were some amazing secret, it's just VERY simple math. This is a community programming forum, not Fort Fucking Knox.

Say you have an image that is 500x250 pixels in dimension. You want to reduce the width to 300. You do OriginalWidth / NewWidth to get the Ratio. You then do OriginalHeight / Ratio and that will give you your NewWidth.

A little bit of pseudocode:
OriginalWidth = Image.Width;
OriginalHeight = Image.Height;
NewWidth = 300; // Resize any input image to 300 pixels in width, proportionally.
Ratio = OriginalWidth / NewWidth;
NewHeight = OriginalHeight / Ratio;
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Nov 9th, 2005, 11:48 PM   #5
bja888
Hobbyist Programmer
 
bja888's Avatar
 
Join Date: Oct 2005
Location: Central, FL
Posts: 213
Rep Power: 0 bja888 is an unknown quantity at this point
Send a message via AIM to bja888 Send a message via Yahoo to bja888
I took a lot out. My scipt is a bit more fancy. I'll give you this code no proublem.
decimal maxSize = 30;
string Filename = "somthing.jpg"
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(logFunk.ThumbnailCallback);
Bitmap myBitmap = new Bitmap(Filename);
System.Drawing.Image myThumbnail;
Size wah = myBitmap.Size;

if(maxSize != null){
int newW = 0;
int newH = 0;
decimal pO = 0;
decimal pT = 0;
bool nopass = false;


if(wah.Width >= wah.Height){
	if(wah.Width > decimal.ToInt32(maxSize)){
		decimal temp_d = decimal.Parse((decimal.Parse(wah.Width.ToString()) / 100).ToString());
		pT = (maxSize / temp_d);
		pO = (decimal.Parse(wah.Height.ToString()) / 100);
		temp_d = (pO * pT);
		newH = decimal.ToInt32(temp_d);
		newW = decimal.ToInt32(maxSize);
	}else{
		nopass = true;
	}
}else{
	if(wah.Height > decimal.ToInt32(maxSize)){
		decimal temp_d = decimal.Parse((decimal.Parse(wah.Height.ToString()) / 100).ToString());
		pT = (maxSize / temp_d);
		pO = (decimal.Parse(wah.Width.ToString()) / 100);
		temp_d = (pO * pT);
		newW = decimal.ToInt32(temp_d);
		newH = decimal.ToInt32(maxSize);
	}else{
		nopass = true;
	}
}

if(nopass){newW = wah.Width; newH = wah.Height;}
myThumbnail = myBitmap.GetThumbnailImage(newW, newH, myCallback, IntPtr.Zero);

}else{
myThumbnail = myBitmap.GetThumbnailImage(wah.Width, wah.Height, myCallback, IntPtr.Zero);
}
myThumbnail.Save("output.jpg");
bja888 is offline   Reply With Quote
Old Nov 10th, 2005, 1:33 AM   #6
Ghost
Man Bear Pig Hunter
 
Ghost's Avatar
 
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4 Ghost is on a distinguished road
Thanks guys, turns out the trouble I ran into was the file access error, because it only returned generic GDI+ error, doesnt help a lot but I finally got around to it.

The code is at my office, so I'll post it if any of you guys care to give me some tips on how I can do it better or maybe see what I was trying to do, because sometimes I don't explain it very well. Thanks again for both of your replies.
Ghost is offline   Reply With Quote
Old Nov 10th, 2005, 2:27 PM   #7
Ghost
Man Bear Pig Hunter
 
Ghost's Avatar
 
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4 Ghost is on a distinguished road
Call the Resize:
string logoFileName = (LogoFile == string.Empty) ? "images/LogoAlignmentLogo.jpg" : LogoFile;
int ndx = logoFileName.LastIndexOf(LOGO_DIRECTORY + "/");
if(ndx > 0)
	logoFileName = logoFileName.Substring(ndx);
if (System.IO.File.Exists(Server.MapPath(logoFileName)))
{
	Bitmap bitmap = new Bitmap(Server.MapPath(logoFileName));

	// Set Size
	int Height = 100;
	int Width = 475;
	if (bitmap.Width > Width || bitmap.Height > Height)
	{
		ResizeImage(bitmap.Width, bitmap.Height, ref Width, ref Height);
		ImageLogo.Width  = Width;
		ImageLogo.Height = Height;
	}
	else
	{
		ImageLogo.Width  = bitmap.Width;
		ImageLogo.Height = bitmap.Height;
	}

	ImageLogo.ImageUrl = Server.MapPath(logoFileName);
	bitmap.Dispose();
}
else
{
	ImageLogo.Width  = 0;
	ImageLogo.Height = 0;
}

Resize:
private void ResizeImage(int sourceWidth, int sourceHeight, ref int width, ref int height)
{
int Width = width; 
int Height = height;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)Width/(float)sourceWidth);
nPercentH = ((float)Height/(float)sourceHeight);
if(nPercentH < nPercentW)
{
	nPercent = nPercentH;
}
else
{
	nPercent = nPercentW;
}
width = (int)(sourceWidth * nPercent);
height = (int)(sourceHeight * nPercent);
}

And then the really fun one.
Resize and Draw:
if(RequiredFieldValidatorLocalFile.IsValid && CustomValidatorLocalFile.IsValid)
{
string extension = Path.GetExtension(LocalFile.PostedFile.FileName);
string path = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory) + LOGO_DIRECTORY;
string logoFile = string.Format("{0}/Logo{1}{2}", path, UserInfo.SubscriberID, extension);
try
{
LocalFile.PostedFile.SaveAs(logoFile.Substring(0,logoFile.Length - 4) + ".jpg");
LogoFile = logoFile.Substring(0,logoFile.Length - 4) + ".jpg";

string logoFileName = logoFile.Substring(0,logoFile.Length - 4) + ".jpg";;
int ndx = logoFileName.LastIndexOf(LOGO_DIRECTORY + "/");
if(ndx > 0)
logoFileName = logoFileName.Substring(ndx);

if (System.IO.File.Exists(Server.MapPath(logoFileName)))
{
Bitmap bitmap = new Bitmap(Server.MapPath(logoFileName));
int Width = 600;
int Height = 150;
if (bitmap.Width > Width || bitmap.Height > Height)
{
FixSize(Server.MapPath(logoFileName.Substring(0,logoFileName.Length - 4) + "_temp.jpg"), bitmap, Width, Height);
}
else
{
FixSize(Server.MapPath(logoFileName.Substring(0,logoFileName.Length - 4) + "_temp.jpg"), bitmap, bitmap.Width, bitmap.Height);
}

ImageLogo.ImageUrl = Server.MapPath(logoFileName);
bitmap.Dispose();
// Delete Original and Rename Temp
File.Delete(Server.MapPath(logoFileName));

File.Move(Server.MapPath(logoFileName.Substring(0,logoFileName.Length - 4) + "_temp.jpg"),Server.MapPath(logoFileName.Substring(0,logoFileName.Length - 4) + ".jpg"));

logoFileName = logoFileName.Substring(logoFileName.LastIndexOf(LOGO_DIRECTORY + "/"),(logoFileName.Length - logoFileName.LastIndexOf(LOGO_DIRECTORY + "/")));

}

}

private void FixSize(string SavePath, System.Drawing.Bitmap 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 bitmapResized = new Bitmap(destWidth, destHeight, 
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bitmapResized.SetResolution(imgPhoto.HorizontalResolution, 
imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bitmapResized);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = 
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto, 
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();

bitmapResized.Save(SavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
Ghost is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC