C\#이미지 처리 3 가지 고급 응용
고 축약도 선명 도
이전 에는 미리 보기 그림 이 원래 그림 보다 뚜렷 할 수 없다 고 생각 했 는데,어느 날 한 제품 의 동료 가 나 에 게 어떤 사이트 의 효 과 를 보 여 주 었 다.그래서.NET 에서 실현 코드 를 찾기 시 작 했 습 니 다.미리 보기 그림 을 자세히 살 펴 보면 원래 그림 보다 더 뚜렷 해 졌 습 니 다.그러나 대 가 는 미리 보기 그림 파일 이 원래 그림 보다 더 크기 때 문 입 니 다.따라서 디 스 플레이 화면 을 가득 채운 초대형 그림 을 더욱 선명 하 게 하려 면 그림 이 공간 과 네트워크 흐름 을 차지 하 는 것 을 고려 해 야 합 니 다.만약 에 인터넷 응용 이 라면미리 보기 그림 을 200 픽 셀 이내 에 사용 하 는 것 을 권장 합 니 다.물론 더 좋 은 코드 가 있다 면 그림 파일 의 크기 를 크게 변화 시 키 지 않 고 그림 을 더욱 선명 하 게 공유 할 수 있 습 니 다.
그림 편집
알고리즘:원 도 중심 을 재단 중심 으로 원 도 를 최대 범위 로 재단 한 다음 에 재단 결 과 를 크기 조정 합 니 다.
사진 인쇄
자세 한 코드:
몇 가지 통용 되 는 방법 을 봉 하 였 는데,만약 버그 나 구멍 이 발견 되면 제때에 바로잡아 주 십시오.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace WuJian.Common
{
public class Image
{
#region
/// <summary>
///
/// , ,
///
/// </summary>
/// <remarks> 2012-08-08</remarks>
/// <param name="fromFile"> Stream </param>
/// <param name="fileSaveUrl"> </param>
/// <param name="side"> ( )</param>
/// <param name="quality"> ( 0-100)</param>
public static void CutForSquare(System.IO.Stream fromFile, string fileSaveUrl, int side, int quality)
{
//
string dir = Path.GetDirectoryName(fileSaveUrl);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// ( , )
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
// , ,
if (initImage.Width <= side && initImage.Height <= side)
{
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
// 、
int initWidth = initImage.Width;
int initHeight = initImage.Height;
//
if (initWidth != initHeight)
{
//
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//
if (initWidth > initHeight)
{
//
pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
//
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//
initWidth = initHeight;
}
//
else
{
//
pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
//
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//
initHeight = initWidth;
}
//
initImage = (System.Drawing.Image)pickedImage.Clone();
//
pickedG.Dispose();
pickedImage.Dispose();
}
//
System.Drawing.Image resultImage = new System.Drawing.Bitmap(side, side);
System.Drawing.Graphics resultG = System.Drawing.Graphics.FromImage(resultImage);
//
resultG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
resultG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
resultG.Clear(Color.White);
//
resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
//
// , jpeg,bmp,png,gif,tiff
ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in icis)
{
if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
{
ici = i;
}
}
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
//
resultImage.Save(fileSaveUrl, ici, ep);
//
ep.Dispose();
//
resultG.Dispose();
resultImage.Dispose();
//
initImage.Dispose();
}
}
#endregion
#region
/// <summary>
///
///
/// </summary>
/// <remarks> 2012-08-08</remarks>
/// <param name="fromFile"> Stream </param>
/// <param name="fileSaveUrl"> </param>
/// <param name="maxWidth"> ( :px)</param>
/// <param name="maxHeight"> ( :px)</param>
/// <param name="quality"> ( 0-100)</param>
public static void CutForCustom(System.IO.Stream fromFile, string fileSaveUrl, int maxWidth, int maxHeight, int quality)
{
// ,
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
// , ,
if (initImage.Width <= maxWidth && initImage.Height <= maxHeight)
{
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
//
double templateRate = (double)maxWidth / maxHeight;
//
double initRate = (double)initImage.Width / initImage.Height;
// ,
if (templateRate == initRate)
{
//
System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);
System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, maxWidth, maxHeight), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
// ,
else
{
//
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
//
Rectangle fromR = new Rectangle(0, 0, 0, 0);//
Rectangle toR = new Rectangle(0, 0, 0, 0);//
//
if (templateRate > initRate)
{
//
pickedImage = new System.Drawing.Bitmap(initImage.Width, (int)System.Math.Floor(initImage.Width / templateRate));
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
//
fromR.X = 0;
fromR.Y = (int)System.Math.Floor((initImage.Height - initImage.Width / templateRate) / 2);
fromR.Width = initImage.Width;
fromR.Height = (int)System.Math.Floor(initImage.Width / templateRate);
//
toR.X = 0;
toR.Y = 0;
toR.Width = initImage.Width;
toR.Height = (int)System.Math.Floor(initImage.Width / templateRate);
}
//
else
{
pickedImage = new System.Drawing.Bitmap((int)System.Math.Floor(initImage.Height * templateRate), initImage.Height);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
fromR.X = (int)System.Math.Floor((initImage.Width - initImage.Height * templateRate) / 2);
fromR.Y = 0;
fromR.Width = (int)System.Math.Floor(initImage.Height * templateRate);
fromR.Height = initImage.Height;
toR.X = 0;
toR.Y = 0;
toR.Width = (int)System.Math.Floor(initImage.Height * templateRate);
toR.Height = initImage.Height;
}
//
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//
System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);
System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(pickedImage, new System.Drawing.Rectangle(0, 0, maxWidth, maxHeight), new System.Drawing.Rectangle(0, 0, pickedImage.Width, pickedImage.Height), System.Drawing.GraphicsUnit.Pixel);
//
// , jpeg,bmp,png,gif,tiff
ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in icis)
{
if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")
{
ici = i;
}
}
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
//
templateImage.Save(fileSaveUrl, ici, ep);
//templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
//
templateG.Dispose();
templateImage.Dispose();
pickedG.Dispose();
pickedImage.Dispose();
}
}
//
initImage.Dispose();
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <remarks> 2012-08-08</remarks>
/// <param name="fromFile"> Stream </param>
/// <param name="savePath"> </param>
/// <param name="targetWidth"> </param>
/// <param name="targetHeight"> </param>
/// <param name="watermarkText"> ( "" )</param>
/// <param name="watermarkImage"> ( "" )</param>
public static void ZoomAuto(System.IO.Stream fromFile, string savePath, System.Double targetWidth, System.Double targetHeight, string watermarkText, string watermarkImage)
{
//
string dir = Path.GetDirectoryName(savePath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// ( , )
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
// , ,
if (initImage.Width <= targetWidth && initImage.Height <= targetHeight)
{
//
if (watermarkText != "")
{
using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(initImage))
{
System.Drawing.Font fontWater = new Font(" ", 10);
System.Drawing.Brush brushWater = new SolidBrush(Color.White);
gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
gWater.Dispose();
}
}
//
if (watermarkImage != "")
{
if (File.Exists(watermarkImage))
{
//
using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
{
// :
if (initImage.Width >= wrImage.Width && initImage.Height >= wrImage.Height)
{
Graphics gWater = Graphics.FromImage(initImage);
//
ImageAttributes imgAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},// :0.5
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gWater.DrawImage(wrImage, new Rectangle(initImage.Width - wrImage.Width, initImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);
gWater.Dispose();
}
wrImage.Dispose();
}
}
}
//
initImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
// 、
double newWidth = initImage.Width;
double newHeight = initImage.Height;
// ( )
if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
{
//
if (initImage.Width > targetWidth)
{
// ,
newWidth = targetWidth;
newHeight = initImage.Height * (targetWidth / initImage.Width);
}
}
// ( )
else
{
//
if (initImage.Height > targetHeight)
{
// ,
newHeight = targetHeight;
newWidth = initImage.Width * (targetHeight / initImage.Height);
}
}
//
// bmp
System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
//
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
//
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
newG.Clear(Color.White);
//
newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
//
if (watermarkText != "")
{
using (System.Drawing.Graphics gWater = System.Drawing.Graphics.FromImage(newImage))
{
System.Drawing.Font fontWater = new Font(" ", 10);
System.Drawing.Brush brushWater = new SolidBrush(Color.White);
gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
gWater.Dispose();
}
}
//
if (watermarkImage != "")
{
if (File.Exists(watermarkImage))
{
//
using (System.Drawing.Image wrImage = System.Drawing.Image.FromFile(watermarkImage))
{
// :
if (newImage.Width >= wrImage.Width && newImage.Height >= wrImage.Height)
{
Graphics gWater = Graphics.FromImage(newImage);
//
ImageAttributes imgAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.5f, 0.0f},// :0.5
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gWater.DrawImage(wrImage, new Rectangle(newImage.Width - wrImage.Width, newImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);
gWater.Dispose();
}
wrImage.Dispose();
}
}
}
//
newImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
//
newG.Dispose();
newImage.Dispose();
initImage.Dispose();
}
}
#endregion
#region
/// <summary>
/// WEB
/// ( :JPG,GIF,BMP,PNG)
/// </summary>
/// <param name="contentType">HttpPostedFile.ContentType</param>
/// <returns></returns>
public static bool IsWebImage(string contentType)
{
if (contentType == "image/pjpeg" || contentType == "image/jpeg" || contentType == "image/gif" || contentType == "image/bmp" || contentType == "image/png")
{
return true;
}
else
{
return false;
}
}
#endregion
}//end class
}
이상 은 C\#이미지 처리 의 고급 응용 프로그램 입 니 다.여러분 의 학습 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.