我创建图像调整大小类,图像调整大小但显示黑色rectengular框。 [英] I create image resize class and image is resize but display black rectengular box.
本文介绍了我创建图像调整大小类,图像调整大小但显示黑色rectengular框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的.CS文件代码
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 Services.MyClassFiles;
使用 System.Drawing;
使用 System.Web.UI.WebControls;
命名空间 MyClasses
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}
public System.Drawing.Image GetImage1( string 路径, int 高度, int width, Double upWidth, Double upHeight)
{
ImageResize1 dataservice = new ImageResize1();
System.Drawing.Image images = dataservice.ScaleImage(System.Drawing.Image.FromFile(Path),Height,width,upWidth,upHeight);
返回图片;
Bitmap newBmp = new 位图(images.Width,images.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}
受保护 void btn_save_Click( object sender,System.EventArgs e)
{
ImageResize1 dataservice = new ImageResize1();
if (fileupload1.HasFile)
{
string fname = ; string fpath = ;
fname = fileupload1.FileName;
fpath = Server.MapPath( 〜/ image /)+ fname;
fileupload1.SaveAs(fpath);
Bitmap upBmp =(Bitmap)Bitmap.FromStream(fileupload1.PostedFile.InputStream);
Double upWidth = upBmp.Width;
Double upHeight = upBmp.Height;
System.Drawing.Image images = GetImage1(fpath, 500 , 500 ,upWidth,upHeight );
System.Drawing.Image images1 = GetImage1(fpath, 50 , 50 ,upWidth,upHeight );
System.Drawing.Image images2 = GetImage1(fpath, 200 , 500 ,upWidth,upHeight );
字符串 upName = fname.Substring( 0 ,fname.IndexOf(< span class =code-string> 。));
string repath = 〜/ thumb / + upName + 。jpg;
string repath1 = 〜/ small / + upName + 。jpg;
string repath2 = 〜/ d / + upName + 。jpg;
images.Save(MapPath(repath),System.Drawing.Imaging.ImageFormat.Jpeg);
images1.Save(MapPath(repath1),System.Drawing.Imaging.ImageFormat.Jpeg);
images2.Save(MapPath(repath2),System.Drawing.Imaging.ImageFormat.Jpeg);
img.ImageUrl = repath2;
}
}
}
}
我的班级文件代码
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.IO;
使用 System.Net;
使用 System.Drawing;
使用 System.Drawing.Drawing2D;
使用 System.Drawing.Imaging;
命名空间 Services.MyClassFiles
{
public class ImageResize1
{
public System.Drawing.Image ScaleImage (System.Drawing.Image srcImage, int newWidth, int newHeight, Double upWidth, Double upHeight)
{
int newimagewidth = newWidth;
int newimageheight = newHeight;
int x = 0 ;
int y = 0 ;
Double reduce;
if (upWidth > upHeight)
{
reduce = newimagewidth / upWidth;
newimageheight =(( Int32 )(upHeight * reduce));
y =(( Int32 )((newHeight - newimageheight)/ 2 ));
x = 0 ;
}
如果(upWidth < upHeight)
{
reduce = newimageheight / upHeight;
newimagewidth =(( Int32 )(upWidth * reduce));
x =(( Int32 )((newWidth - newimagewidth)/ 2 ) );
y = 0 ;
}
// if(Double upHeight = Double upWidth)
// {
// reduce = newimageheight / upHeight;
// newimagewidth =((Int32)(upWidth * reduce));
// x =((Int32)((newWidth - newimagewidth)/ 2));
// x =((Int32)((newHeight - newimageheight)/ 2));
// }
位图newBmp = 新位图(newimagewidth,newimageheight,Syste m.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution( 72 , 72 );
// System.Drawing.Image newImage = new Bitmap(newWidth,newHeight);
使用(Graphics gr = Graphics.FromImage(newBmp))
{
// gr.Clear(Color.White);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.DrawImage(srcImage, new Rectangle(x,y,newimagewidth,newimageheight));
}
return newBmp;
}
}
}
解决方案
在类文件代码中
gr.DrawImage(srcImage, new 矩形(x,y,newimagewidth,newimageheight));
替换为
gr.DrawImage(srcImage, new 矩形( 0 , 0 ,newimagewidth,newimageheight));
my .CS file Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Services.MyClassFiles;
using System.Drawing;
using System.Web.UI.WebControls;
namespace MyClasses
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public System.Drawing.Image GetImage1(string Path, int Height, int width, Double upWidth, Double upHeight)
{
ImageResize1 dataservice = new ImageResize1();
System.Drawing.Image images = dataservice.ScaleImage(System.Drawing.Image.FromFile(Path), Height, width, upWidth, upHeight);
return images;
Bitmap newBmp = new Bitmap(images.Width, images.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}
protected void btn_save_Click(object sender, System.EventArgs e)
{
ImageResize1 dataservice = new ImageResize1();
if (fileupload1.HasFile)
{
string fname = ""; string fpath = "";
fname = fileupload1.FileName;
fpath = Server.MapPath("~/image/") + fname;
fileupload1.SaveAs(fpath);
Bitmap upBmp = (Bitmap)Bitmap.FromStream(fileupload1.PostedFile.InputStream);
Double upWidth = upBmp.Width;
Double upHeight = upBmp.Height;
System.Drawing.Image images = GetImage1(fpath, 500, 500, upWidth, upHeight);
System.Drawing.Image images1 = GetImage1(fpath, 50, 50, upWidth, upHeight);
System.Drawing.Image images2 = GetImage1(fpath, 200, 500, upWidth, upHeight);
String upName = fname.Substring(0, fname.IndexOf("."));
string repath = "~/thumb/" + upName + ".jpg";
string repath1 = "~/small/" + upName + ".jpg";
string repath2 = "~/d/" + upName + ".jpg";
images.Save(MapPath(repath), System.Drawing.Imaging.ImageFormat.Jpeg);
images1.Save(MapPath(repath1), System.Drawing.Imaging.ImageFormat.Jpeg);
images2.Save(MapPath(repath2), System.Drawing.Imaging.ImageFormat.Jpeg);
img.ImageUrl = repath2;
}
}
}
}
My Class file code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Services.MyClassFiles
{
public class ImageResize1
{
public System.Drawing.Image ScaleImage(System.Drawing.Image srcImage, int newWidth, int newHeight, Double upWidth,Double upHeight )
{
int newimagewidth = newWidth;
int newimageheight = newHeight;
int x = 0;
int y = 0;
Double reduce;
if (upWidth > upHeight)
{
reduce = newimagewidth / upWidth;
newimageheight = ((Int32)(upHeight * reduce));
y = ((Int32)((newHeight - newimageheight) / 2));
x = 0;
}
if (upWidth < upHeight)
{
reduce = newimageheight / upHeight;
newimagewidth = ((Int32)(upWidth * reduce));
x = ((Int32)((newWidth - newimagewidth) / 2));
y = 0;
}
//if (Double upHeight = Double upWidth)
//{
// reduce = newimageheight/ upHeight;
// newimagewidth = ((Int32)(upWidth * reduce));
// x = ((Int32)((newWidth - newimagewidth) / 2));
// x = ((Int32)((newHeight - newimageheight) / 2));
//}
Bitmap newBmp = new Bitmap(newimagewidth, newimageheight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
//System.Drawing.Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newBmp))
{
//gr.Clear(Color.White);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.DrawImage(srcImage, new Rectangle(x, y, newimagewidth, newimageheight));
}
return newBmp;
}
}
}
解决方案
In Class File code
gr.DrawImage(srcImage, new Rectangle(x, y, newimagewidth, newimageheight));
Replace with
gr.DrawImage(srcImage, new Rectangle(0, 0, newimagewidth, newimageheight));
这篇关于我创建图像调整大小类,图像调整大小但显示黑色rectengular框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文