?? imageutility.cs
字號:
SqlCommand cmdAdd = new SqlCommand("Community_ImagesUpdateSectionImage", conPortal);
cmdAdd.CommandType = CommandType.StoredProcedure;
cmdAdd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
cmdAdd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmdAdd.Parameters.Add("@imageID", imageID);
cmdAdd.Parameters.Add("@imageName", imgFileName);
cmdAdd.Parameters.Add("@contentType", imgContentType);
cmdAdd.Parameters.Add("@imageData", SqlDbType.Image).Value = imgBinaryData;
conPortal.Open();
cmdAdd.ExecuteNonQuery();
conPortal.Close();
}
//*********************************************************************
//
// EditCommunityImage Method
//
// Updates a community image in the database.
//
//*********************************************************************
public static int EditCommunityImage(string originalFileName, CommunityImageType imageType, HttpPostedFile proposedFile) {
string imgFileName = proposedFile.FileName.Remove(0,proposedFile.FileName.LastIndexOf("\\")+1);
string imgContentType = proposedFile.ContentType;
if (imgContentType == "image/jpeg")
{
imgContentType = "image/pjpeg";
}
// Process the image into byte array
Stream imgStream = proposedFile.InputStream;
int imgLen = proposedFile.ContentLength;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);
// Add the image to the database
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmdEdit = new SqlCommand("Community_ImagesEditCommunityImage", conPortal);
cmdEdit.CommandType = CommandType.StoredProcedure;
cmdEdit.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
cmdEdit.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmdEdit.Parameters.Add("@originalFileName", originalFileName);
cmdEdit.Parameters.Add("@fileName", imgFileName);
cmdEdit.Parameters.Add("@contentType", imgContentType);
cmdEdit.Parameters.Add("@imageType", imageType);
cmdEdit.Parameters.Add("@imageData", SqlDbType.Image).Value = imgBinaryData;
conPortal.Open();
int result = cmdEdit.ExecuteNonQuery();
conPortal.Close();
return result;
}
//*********************************************************************
//
// BuildImagePath Method
//
// Builds a URL to a section image given the image ID and image name.
//
//*********************************************************************
public static string BuildImagePath(int id, string fileName) {
string extension = Path.GetExtension(fileName).ToLower();
return id.ToString() + extension;
}
//*********************************************************************
//
// BuildFullImagePath Method
//
// Builds a URL to a section image that includes the section path.
//
//*********************************************************************
public static string BuildFullImagePath(int sectionID, int imageID, string fileName) {
string path = SectionUtility.GetSectionPath(sectionID);
path = path.Remove( path.LastIndexOf("/"), path.Length - path.LastIndexOf( "/" ) );
string extension = Path.GetExtension(fileName).ToLower();
return String.Format("{0}/{1}{2}", path, imageID, extension);
}
//*********************************************************************
//
// IsImageFile Method
//
// Returns true or false depending on whether the filename
// ends with an image file extension.
//
//*********************************************************************
public static bool IsImageFile(string fileName) {
fileName = fileName.ToLower();
if (fileName.EndsWith(".gif") || fileName.EndsWith(".jpg") || fileName.EndsWith(".jpeg"))
return true;
return false;
}
//*********************************************************************
//
// ModifyImage Method
//
// Modifies the width or height of an image.
//
//*********************************************************************
public static byte[] ModifyImage (
byte[] original,
int width,
int height,
string contentType,
bool isThumbNail)
// original: byte array of the image
// width: new image width
// height: new image height
// contentType: type of image (this should be either pjpeg or gif)
// isThumbnail:
{
// convert byte array to image
MemoryStream stream = new MemoryStream(original);
//
// convert size to new dimensions
Bitmap bmp = (Bitmap) Image.FromStream (stream);
// Calculate missing width or height
if (width == -1)
width = (int)( ((double)height / (double)bmp.Height) * (double)bmp.Width );
if (height == -1)
height = (int)( ((double)width / (double)bmp.Width) * (double)bmp.Height );
Bitmap thumbBmp = new Bitmap( bmp,width,height);
thumbBmp.Palette = GetTransparentColorPalette(bmp);
// convert to stream in preparation to convert to byte array
stream = new MemoryStream();
// save the stream
// first we need to determine if image is JPG or GIF
// to determine the proper encoder type
if (contentType.ToLower().EndsWith ("pjpeg")) {
// is this a thumbnail?
if (isThumbNail)
{
stream = SaveJpgWithCompression (thumbBmp,40);
} else {
// thumbBmp.Save ("C:\\atest\\savingtest.jpg",ImageFormat.Jpeg);
thumbBmp.Save (stream, ImageFormat.Jpeg);
}
} else {
//thumbBmp.Save ("C:\\atest\\savingtest.gif",ImageFormat.Gif);
thumbBmp.Save (stream, ImageFormat.Gif);
//tranparency is not supported in IE for png files
}
// cleanup
thumbBmp.Dispose();
bmp.Dispose();
return stream.GetBuffer();
}
//*********************************************************************
//
// SaveJpgWithCompression Method
//
// Compresses a JPEG image.
//
//*********************************************************************
private static void SaveJpgWithCompression( Image original, string fileName, long compression )
// original: The image to compress
// fileName: The filename to save the JPG to.
// compression: A value from 1 to 100 A value of 100 is no compression.
// A value of 1 is maximum compression.
// Note. JPG's upload by user will likely have some compression
// Therefore a value of 40 or more will be needed to see some
// improvements in file size
{
EncoderParameters eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter( Encoder.Quality, compression );
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
original.Save( fileName, ici, eps );
}
private static MemoryStream SaveJpgWithCompression( Image original, long compression )
// original: The image to compress
// compression: A value from 1 to 100 A value of 100 is no compression.
// A value of 1 is maximum compression.
// Note. JPG's upload by user will likely have some compression
// Therefore a value of 40 or more will be needed to see some
// improvements in file size
{
MemoryStream ms = new MemoryStream();
EncoderParameters eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter( Encoder.Quality, compression );
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
//original.Save ("C:\\atest\\compresstest.jpg",ici,eps);
original.Save( ms, ici, eps );
return ms;
}
//*********************************************************************
//
// GetTransparentColorPalette Method
//
// Finds the transparent colors in a GIF image.
//
//*********************************************************************
private static ColorPalette GetTransparentColorPalette (Bitmap original) {
Color testColor;
Color newColor;
ColorPalette pal = original.Palette;
for (int i =0; i<pal.Entries.Length-1;i++) {
testColor = pal.Entries[i];
if (testColor.A== 0 ) {
newColor = Color.FromArgb(0,testColor) ;
pal.Entries[i]= newColor;
}
}
return pal;
}
//*********************************************************************
//
// GetEncoderInfo Method
//
// Retrieves the the Image Codec for a particular image MIME type.
//
//*********************************************************************
private static ImageCodecInfo GetEncoderInfo(String mimeType) {
// from KB article Q324790
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j) {
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -