如何将图像隐藏为印度护照尺寸图像 [英] how to covert image as Indian passport size image
问题描述
您好
我在c#中创建应用程序。
我有picturebox1 我将Image1.jpg添加到图片框中
现在我想将Image1转换为印度护照尺寸图像(Image2.jpg)和
将其分配给picturebox2
现在我保存picturebox2(Image2.jpg) 进入sql server databae
请帮帮我
问候
Ramachandran
嗨chandran,
我有一些代码你可以使用
它会调整您选择的图片大小,然后将其另存为gif或jpeg。这是一个使用System的ststic class
;
使用System.Collections.Generic;使用System.Drawing
;
使用System.Drawing.Drawing2D;
使用System.Drawing.Imaging;
使用System.Linq;
//使用Encoder = System.Text.Encoder;
namespace Your_Namespace
{
public static class ImageExtensionMethods
{
static private ImageCodecInfo GetEncoder(ImageFormat format)
{
返回ImageCodecInfo.GetImageDecoders()。SingleOrDefault(c => c.FormatID == format.Guid);
}
public static void SaveAsJpeg(此Image Img,字符串FileName,Int64质量)
{
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
Encoder QualityEncoder = Encoder.Quality;
using(EncoderParameters EP = new EncoderParameters(1))
{
using(EncoderParameter QualityEncoderParameter = new EncoderParameter(QualityEncoder,Quality))
{
EP.Param [0] = QualityEncoderParameter;
Img.Save(FileName,jgpEncoder,EP);
}
}
}
public static void SaveAsGif(此图片Img,字符串FileName,Int64质量)
{
ImageCodecInfo gifEncoder = GetEncoder(ImageFormat.Gif);
Encoder QualityEncoder = Encoder.Quality;
using(EncoderParameters EP = new EncoderParameters(1))
{
using(EncoderParameter QualityEncoderParameter = new EncoderParameter(QualityEncoder,Quality))
{
EP.Param [0] = QualityEncoderParameter;
Img.Save(FileName,gifEncoder,EP);
}
}
}
公共静态图像大小调整(此图像Img,int宽度,int高度,InterpolationMode InterpolationMode)
{
Image CropedImage =新位图(宽度,高度);
使用(Graphics G = Graphics.FromImage(CropedImage))
{
G.SmoothingMode = SmoothingMode.HighQuality;
G.InterpolationMode = InterpolationMode;
G.PixelOffsetMode = PixelOffsetMode.HighQuality;
G.DrawImage(Img,0,0,Width,Height);
}
返回CropedImage;
}
公共静态图像大小调整(此图像Img,int宽度,int高度)
{
返回Img.Resize(宽度,高度,InterpolationMode.HighQualityBicubic) ;
}
private static Rectangle EnsureAspectRatio(此Image Image,int Width,int Height)
{
float AspectRatio = Width /(float)Height;
float CalculatedWidth = Image.Width,CalculatedHeight = Image.Height;
if(Image.Width> = Image.Height)
{
if(Width> Height)
{
CalculatedHeight = Image.Width / AspectRatio;
if(CalculatedHeight> Image.Height)
{
CalculatedHeight = Image.Height;
CalculatedWidth = CalculatedHeight * AspectRatio;
}
}
其他
{
CalculatedWidth = Image.Height * AspectRatio;
if(CalculatedWidth> Image.Width)
{
CalculatedWidth = Image.Width;
CalculatedHeight = CalculatedWidth / AspectRatio;
}
}
}
其他
{
if(宽度<高度)
{
CalculatedHeight = Image.Width / AspectRatio;
if(CalculatedHeight> Image.Height)
{
CalculatedHeight = Image.Height;
CalculatedWidth = CalculatedHeight * AspectRatio;
}
}
其他
{
CalculatedWidth = Image.Height * AspectRatio;
if(CalculatedWidth> Image.Width)
{
CalculatedWidth = Image.Width;
CalculatedHeight = CalculatedWidth / AspectRatio;
}
}
}
返回Rectangle.Ceiling(new RectangleF((Image.Width - CalculatedWidth)/ 2,0,CalculatedWidth,CalculatedHeight));
}
public static Image ResizeToCanvas(this Image Img,int Width,int Height,out Rectangle CropRectangle)
{
return Img.ResizeToCanvas(Width,Height, InterpolationMode.HighQualityBicubic,out CropRectangle);
}
public static Image ResizeToCanvas(此Image Img,int Width,int Height,InterpolationMode InterpolationMode,out Rectangle CropRectangle)
{
CropRectangle = EnsureAspectRatio(Img,宽度,高度);
Image CropedImage =新位图(宽度,高度);
使用(Graphics G = Graphics.FromImage(CropedImage))
{
G.SmoothingMode = SmoothingMode.HighQuality;
G.InterpolationMode = InterpolationMode;
G.PixelOffsetMode = PixelOffsetMode.HighQuality;
G.DrawImage(Img,new Rectangle(0,0,Width,Height),CropRectangle,GraphicsUnit.Pixel);
}
返回CropedImage;
}
public static Image ResizeToCanvas(此Image Img,int Width,int Height,RectangleF CR)
{
return Img.ResizeToCanvas(Width,Height,InterpolationMode) .HighQualityBicubic,CR);
}
public static Image ResizeToCanvas(此Image Img,int Width,int Height,InterpolationMode InterpolationMode,RectangleF CR)
{
Image CropedImage = new Bitmap(Width) ,身高);
使用(Graphics G = Graphics.FromImage(CropedImage))
{
G.SmoothingMode = SmoothingMode.HighQuality;
G.InterpolationMode = InterpolationMode;
G.PixelOffsetMode = PixelOffsetMode.HighQuality;
G.DrawImage(Img,new Rectangle(0,0,Width,Height),CR,GraphicsUnit.Pixel);
}
返回CropedImage;
}
}
}
使用此选项可调整图像大小。
像这样使用它:
Image img = ImageExtensionMethods.Resize(image,300, 100);
ImageExtensionMethods.SaveAsGif(img,logoPath,65535);
Hi
I am creating application in c#.
I have picturebox1 and i add a Image1.jpg in to picture box
Now i want to convert that Image1 to Indian passport size image(Image2.jpg) and
assign it to picturebox2
Now I save the picturebox2 (Image2.jpg) in to sql server databae
Please help me
Regards
Ramachandran
Hi chandran,
I've have some code u can use
It resize a picture of you'r choice and then save it as gif or jpeg. It is a ststic class
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Linq; //using Encoder = System.Text.Encoder; namespace Your_Namespace { public static class ImageExtensionMethods { static private ImageCodecInfo GetEncoder(ImageFormat format) { return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid); } public static void SaveAsJpeg(this Image Img, string FileName, Int64 Quality) { ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg); Encoder QualityEncoder = Encoder.Quality; using (EncoderParameters EP = new EncoderParameters(1)) { using (EncoderParameter QualityEncoderParameter = new EncoderParameter(QualityEncoder, Quality)) { EP.Param[0] = QualityEncoderParameter; Img.Save(FileName, jgpEncoder, EP); } } } public static void SaveAsGif(this Image Img, string FileName, Int64 Quality) { ImageCodecInfo gifEncoder = GetEncoder(ImageFormat.Gif); Encoder QualityEncoder = Encoder.Quality; using (EncoderParameters EP = new EncoderParameters(1)) { using (EncoderParameter QualityEncoderParameter = new EncoderParameter(QualityEncoder, Quality)) { EP.Param[0] = QualityEncoderParameter; Img.Save(FileName, gifEncoder, EP); } } } public static Image Resize(this Image Img, int Width, int Height, InterpolationMode InterpolationMode) { Image CropedImage = new Bitmap(Width, Height); using (Graphics G = Graphics.FromImage(CropedImage)) { G.SmoothingMode = SmoothingMode.HighQuality; G.InterpolationMode = InterpolationMode; G.PixelOffsetMode = PixelOffsetMode.HighQuality; G.DrawImage(Img, 0, 0, Width, Height); } return CropedImage; } public static Image Resize(this Image Img, int Width, int Height) { return Img.Resize(Width, Height, InterpolationMode.HighQualityBicubic); } private static Rectangle EnsureAspectRatio(this Image Image, int Width, int Height) { float AspectRatio = Width / (float)Height; float CalculatedWidth = Image.Width, CalculatedHeight = Image.Height; if (Image.Width >= Image.Height) { if (Width > Height) { CalculatedHeight = Image.Width / AspectRatio; if (CalculatedHeight > Image.Height) { CalculatedHeight = Image.Height; CalculatedWidth = CalculatedHeight * AspectRatio; } } else { CalculatedWidth = Image.Height * AspectRatio; if (CalculatedWidth > Image.Width) { CalculatedWidth = Image.Width; CalculatedHeight = CalculatedWidth / AspectRatio; } } } else { if (Width < Height) { CalculatedHeight = Image.Width / AspectRatio; if (CalculatedHeight > Image.Height) { CalculatedHeight = Image.Height; CalculatedWidth = CalculatedHeight * AspectRatio; } } else { CalculatedWidth = Image.Height * AspectRatio; if (CalculatedWidth > Image.Width) { CalculatedWidth = Image.Width; CalculatedHeight = CalculatedWidth / AspectRatio; } } } return Rectangle.Ceiling(new RectangleF((Image.Width - CalculatedWidth) / 2, 0, CalculatedWidth, CalculatedHeight)); } public static Image ResizeToCanvas(this Image Img, int Width, int Height, out Rectangle CropRectangle) { return Img.ResizeToCanvas(Width, Height, InterpolationMode.HighQualityBicubic, out CropRectangle); } public static Image ResizeToCanvas(this Image Img, int Width, int Height, InterpolationMode InterpolationMode, out Rectangle CropRectangle) { CropRectangle = EnsureAspectRatio(Img, Width, Height); Image CropedImage = new Bitmap(Width, Height); using (Graphics G = Graphics.FromImage(CropedImage)) { G.SmoothingMode = SmoothingMode.HighQuality; G.InterpolationMode = InterpolationMode; G.PixelOffsetMode = PixelOffsetMode.HighQuality; G.DrawImage(Img, new Rectangle(0, 0, Width, Height), CropRectangle, GraphicsUnit.Pixel); } return CropedImage; } public static Image ResizeToCanvas(this Image Img, int Width, int Height, RectangleF CR) { return Img.ResizeToCanvas(Width, Height, InterpolationMode.HighQualityBicubic, CR); } public static Image ResizeToCanvas(this Image Img, int Width, int Height, InterpolationMode InterpolationMode, RectangleF CR) { Image CropedImage = new Bitmap(Width, Height); using (Graphics G = Graphics.FromImage(CropedImage)) { G.SmoothingMode = SmoothingMode.HighQuality; G.InterpolationMode = InterpolationMode; G.PixelOffsetMode = PixelOffsetMode.HighQuality; G.DrawImage(Img, new Rectangle(0, 0, Width, Height), CR, GraphicsUnit.Pixel); } return CropedImage; } } }Use this and your image is resized.
use it like this:
Image img = ImageExtensionMethods.Resize(image, 300, 100); ImageExtensionMethods.SaveAsGif(img, logoPath, 65535);
这篇关于如何将图像隐藏为印度护照尺寸图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!