![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#2 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4
![]() |
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; |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#4 | |
|
Professional Programmer
|
Quote:
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;} |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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"); |
|
|
|
|
|
#6 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
Man Bear Pig Hunter
Join Date: Jul 2005
Location: NorCal, USA
Posts: 295
Rep Power: 4
![]() |
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);
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|