在我的论坛中如何控件别人发贴时的链接图片的大小 我的论坛显示信息的地方只有500px 当一个链接图片的宽度大过于500px的时候如何对其进行调整为500px 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ASP.NET上传图片并生成可带版权信息的缩略图 <%@ Page Language="C#" ResponseEncoding="gb2312" %><%@ import Namespace="System" %><%@ import Namespace="System.IO" %><%@ import Namespace="System.Drawing" %><%@ import Namespace="System.Drawing.Imaging" %><script runat="server"> void Page_Load(Object sender, EventArgs e){if(!Page.IsPostBack){ImgPreview.Visible=false;}}void GetThumbnailImage(int width,int height,string strInfo,int left,int right){string file="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1);string newfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1)+".jpg";string strAdd=strInfo;System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));System.Drawing.Image thumbnailImage =oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);Response.Clear();Bitmap output=new Bitmap(thumbnailImage);Graphics g=Graphics.FromImage(output);g.DrawString(strAdd,new Font("Courier New", 14),new SolidBrush(Color.Red),left,right);output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);Response.ContentType = "image/gif";ImgPreview.Visible=true;ImgPreview.ImageUrl=newfile;}bool ThumbnailCallback(){return true;} using System.Drawing;using System.Drawing.Imaging;namespace ifyr.util{class ImageHandler {static void Main(string[] args) {Image sourceImage = Image.FromFile(@"e:\source.jpg");Compress(sourceImage, @"e:\target.jpg");CreateThumb(sourceImage, 120, 160, @"e:\thumb.jpg");sourceImage.Dispose();}/// /// 将图片转换成200k以下的图片/// 即生成800*800以内像素,65%品质的JPEG格式图片/// 请先判断图片大小,如果小于200k,则不需压缩,以免损失图片质量/// 可以改写为输出到OutputStream/// /// 原图/// 压缩图片的保存文件名public static void Compress(Image sourceImage, string filename) {int targetWidth = 800;int targetHeight = 800;if(sourceImage.Height <= 800 && sourceImage.Width <= 800) {targetHeight = sourceImage.Height;targetWidth = sourceImage.Width;} else if(sourceImage.Height > sourceImage.Width) {targetWidth = sourceImage.Width * 800 / sourceImage.Height;} else {targetHeight = sourceImage.Height * 800 / sourceImage.Width;}// 缩放图片Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());Graphics g = Graphics.FromImage(targetImage);g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);g.Dispose();// 设置输出格式EncoderParameters encParams = new EncoderParameters(1);encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 65L);ImageCodecInfo codeInfo = null;ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();foreach(ImageCodecInfo info in codeInfos) {if(info.MimeType.Equals("image/jpeg")) {codeInfo = info;break;}}if(codeInfo != null) {targetImage.Save(filename, codeInfo, encParams);}targetImage.Dispose();}/// /// 生成缩略图/// 可以改写成输出到OutputSteam/// /// 原图/// 缩略图宽度/// 缩略图高度/// 缩略图的保存文件名public static void CreateThumb(Image sourceImage, int thumbWidth, int thumbHeight, string filename) {int tw = thumbWidth;int th = thumbHeight;if(sourceImage.Height < th && sourceImage.Width < tw) {th = sourceImage.Height;tw = sourceImage.Width;} else if(sourceImage.Height / th > sourceImage.Width / tw) {tw = sourceImage.Width * th / sourceImage.Height;} else {th = sourceImage.Height * tw / sourceImage.Width;}// 将图片缩放到一个空白背景的图片上,生成大小一致的缩略图Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppRgb).GetHbitmap());Graphics g = Graphics.FromImage(thumbImage);SolidBrush brush = new SolidBrush(Color.White);g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);g.DrawImage(sourceImage, (thumbWidth - tw) / 2, (thumbHeight - th) / 2, tw, th);g.Dispose();thumbImage.Save(filename, ImageFormat.Jpeg);thumbImage.Dispose();}}} 以下代码放到网页内function imgzoom(o) { if(event.ctrlKey) { var zoom = parseInt(o.style.zoom, 10) || 100; zoom -= event.wheelDelta / 12; if(zoom > 0) { o.style.zoom = zoom + '%'; } return false; } else { return true; }}window.onload = Init;function Init(){ //定义最大宽度 var maxWidth = 550; var maxHeight = 350; // //2005-10-24 by 武眉博,huobazi 防止图片把文章内容页面撑大 //(visit: http://www.aspxboy.com ) // var imgs = document.getElementsByTagName("img"); for(var i=0; i<imgs.length; i++) { var img = imgs[i]; if (img.width > maxWidth) { img.width = maxWidth; if (img.parentElement.tagName != "A") { img.onclick = function(){window.open(this.src)} img.style.cursor = "pointer"; img.style.cursor = "hand"; //img.onmousewheel=function(){imgzoom(this);} //img.alt="点击看大图,或按住Ctrl键滚动鼠标滚轮放大或者缩小"; img.alt=">>-请点击鼠标查看大图-<<"; } } } } .NET架构 答对了 给分 我没遇到过这种错误 新闻系统中中新闻自动分两列显示问题 如何做到让注册用户30天没有登录就自动删除? 问一个很菜的问题 ====>C#的命名空间是个什么概念? 如何在web上动态生成界面(即所有的控件),并动态定义控件执行的操作? 【提问】如何调整VS2008的界面布局【在线放分】 请推荐几本javascrip的书,用于asp.net开发. 不知是哪里错了,请帮忙看一下。 GDI + 后续问题: 当我对Web程序启动编译时,出现了如下错误,请各位高手现身出来指点,不来看,你以后可能也会发生此错误。 写一个数据访问类的问题!!
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
ImgPreview.Visible=false;
}
}
void GetThumbnailImage(int width,int height,string strInfo,int left,int right)
{
string file="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1);
string newfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1)+".jpg";
string strAdd=strInfo;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));
System.Drawing.Image thumbnailImage =
oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Response.Clear();
Bitmap output=new Bitmap(thumbnailImage);
Graphics g=Graphics.FromImage(output);
g.DrawString(strAdd,new Font("Courier New", 14),new SolidBrush(Color.Red),left,right);
output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "image/gif";
ImgPreview.Visible=true;
ImgPreview.ImageUrl=newfile;
}
bool ThumbnailCallback()
{
return true;
}
using System.Drawing.Imaging;namespace ifyr.util{
class ImageHandler {
static void Main(string[] args) {
Image sourceImage = Image.FromFile(@"e:\source.jpg");Compress(sourceImage, @"e:\target.jpg");
CreateThumb(sourceImage, 120, 160, @"e:\thumb.jpg");sourceImage.Dispose();
}///
/// 将图片转换成200k以下的图片
/// 即生成800*800以内像素,65%品质的JPEG格式图片
/// 请先判断图片大小,如果小于200k,则不需压缩,以免损失图片质量
/// 可以改写为输出到OutputStream
///
/// 原图
/// 压缩图片的保存文件名
public static void Compress(Image sourceImage, string filename) {
int targetWidth = 800;
int targetHeight = 800;if(sourceImage.Height <= 800 && sourceImage.Width <= 800) {
targetHeight = sourceImage.Height;
targetWidth = sourceImage.Width;
} else if(sourceImage.Height > sourceImage.Width) {
targetWidth = sourceImage.Width * 800 / sourceImage.Height;
} else {
targetHeight = sourceImage.Height * 800 / sourceImage.Width;
}// 缩放图片
Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(targetImage);
g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);
g.Dispose();
// 设置输出格式
EncoderParameters encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 65L);
ImageCodecInfo codeInfo = null;
ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();
foreach(ImageCodecInfo info in codeInfos) {
if(info.MimeType.Equals("image/jpeg")) {
codeInfo = info;
break;
}
}
if(codeInfo != null) {
targetImage.Save(filename, codeInfo, encParams);
}
targetImage.Dispose();
}///
/// 生成缩略图
/// 可以改写成输出到OutputSteam
///
/// 原图
/// 缩略图宽度
/// 缩略图高度
/// 缩略图的保存文件名
public static void CreateThumb(Image sourceImage, int thumbWidth, int thumbHeight, string filename) {
int tw = thumbWidth;
int th = thumbHeight;if(sourceImage.Height < th && sourceImage.Width < tw) {
th = sourceImage.Height;
tw = sourceImage.Width;
} else if(sourceImage.Height / th > sourceImage.Width / tw) {
tw = sourceImage.Width * th / sourceImage.Height;
} else {
th = sourceImage.Height * tw / sourceImage.Width;
}// 将图片缩放到一个空白背景的图片上,生成大小一致的缩略图
Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(thumbImage);
SolidBrush brush = new SolidBrush(Color.White);
g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
g.DrawImage(sourceImage, (thumbWidth - tw) / 2, (thumbHeight - th) / 2, tw, th);
g.Dispose();thumbImage.Save(filename, ImageFormat.Jpeg);
thumbImage.Dispose();
}
}
}
function imgzoom(o)
{
if(event.ctrlKey)
{
var zoom = parseInt(o.style.zoom, 10) || 100;
zoom -= event.wheelDelta / 12;
if(zoom > 0)
{
o.style.zoom = zoom + '%';
}
return false;
}
else
{
return true;
}
}
window.onload = Init;
function Init()
{
//定义最大宽度
var maxWidth = 550;
var maxHeight = 350;
//
//2005-10-24 by 武眉博,huobazi 防止图片把文章内容页面撑大
//(visit: http://www.aspxboy.com )
//
var imgs = document.getElementsByTagName("img");
for(var i=0; i<imgs.length; i++)
{
var img = imgs[i];
if (img.width > maxWidth)
{
img.width = maxWidth;
if (img.parentElement.tagName != "A")
{
img.onclick = function(){window.open(this.src)}
img.style.cursor = "pointer";
img.style.cursor = "hand";
//img.onmousewheel=function(){imgzoom(this);}
//img.alt="点击看大图,或按住Ctrl键滚动鼠标滚轮放大或者缩小";
img.alt=">>-请点击鼠标查看大图-<<";
}
}
}
}